我有一个定义了函数的文件,它将数据导入并组织到列表列表中。它返回该列表列表,并且所有函数在同一个文件中都很好(如果我编写一个主函数并调用导入函数,没有问题)。
def import_viscosity_data(size_of_header):
    ...
    return (list_of_lists)
我正在尝试使用以下命令从同一目录中的另一个文件调用此函数:
import load_files
print(load_files.import_viscosity_data(7))
不幸的是,这一直返回'None',如果我尝试获取返回数组的长度,它会抛出一个错误:TypeError:'NoneType'类型的对象没有len()
我猜它正在向我传递对列表的引用,并且一旦函数终止,实际列表就会被删除,但我不确定如何解决这个问题。任何帮助将不胜感激!
这是代码:
import os
import tkinter
from tkinter import filedialog
#import decimal
from decimal import *
def import_viscosity_data(size_of_header):
    ### This function imports viscosity data from multiple files, skipping the 
    ### header passed inof the form shearRate '\t' viscosity and puts it into 
    ### an array of the form test_num[result_type[data]]] where result type 
    ### is 0 (shearRate) or 1 (viscosity)
    header_size = size_of_header
    root = tkinter.Tk()
    root.withdraw()
    file_path = root.tk.splitlist(filedialog.askopenfilenames(
        parent=root, title='Choose a file(s):'))
    test_num = []
    result_type = []
    data_1 = []
    data_2 = []
    for file_name in file_path:
        f = open(file_name)
        ## Skip the header, which consists of header_size lines
        for i in range(header_size):
            next(f)
        lines = [line.strip() for line in f]
        f.close()
        ## For a line, slice all characters before the tab, then after the tab
        ## convert to Decimal, and append to the data list
        for index in range(len(lines)):
            data_1.append(Decimal(lines[index][0:lines[index].find('\t')]))
            data_2.append(Decimal(lines[index][lines[index].find('\t') + 1:]))
        result_type.append(data_1)
        result_type.append(data_2)
        test_num.append(result_type)
        data_1, data_2, result_type = [], [], []
    return(test_num)
这里有一些示例数据可供试用(两列中的任何数据,中间有一个制表符):
0   1.2381
0.004   1.23901
0.008   1.23688
0.012   1.23734
0.016   1.23779
0.02    1.23901
0.024   1.23932
0.028   1.23886
0.032   1.23688
0.036   1.2384
同样,在这个程序中(在 IDE 中运行,或者如果我编写了一个小的 main() 函数),这将返回一个列表列表,并且工作得很好。但是,当我将函数导入到不同的文件中时,它会返回 None,而不会引发任何错误。函数名称在 IDE 之后会自动弹出import load_files,因此它似乎可以正确导入。
注意
*此次要问题已解决。该文件load_files.py位于名为load_files. 导入语句已更改为from load_files import load_files,现在可以正常运行。*
今天我的问题变得更糟了。现在,我无法从第一个文件中获取任何要在第二个文件中识别的功能。甚至是一组简单的代码,例如:
#load_files.py
def test_func():
    print('test successful')
#test.py
import load_files
load_files.test_func()
正在抛出此错误:
Traceback (most recent call last):
  File "C:\Users\tmulholland\Documents\Carreau - WLF\test.py", line 8, in <module>
    load_files.test_func
AttributeError: 'module' object has no attribute 'test_func'
load_files.py在它自己的文件夹(同名)中,有一个空白__init__.py文件
注意我应该补充一点,我正在使用 Pyzo IDE,因为我想使用 scipy 库来解决曲线拟合/优化问题。无论多么简单,我今天都无法将任何函数正确导入 Pyzo。其他人有这个问题吗?