python - Passing a list from an imported module -


i have file function defined, imports , organizes data list of lists. returns list of lists, , functions fine within same file (if write main function , call import function, no problems).

def import_viscosity_data(size_of_header):     ...     return (list_of_lists) 

i'm trying call function file in same directory, using following:

import load_files print(load_files.import_viscosity_data(7)) 

unfortunately, keeps returning 'none', , if try length of returned array, throws error:typeerror: object of type 'nonetype' has no len()

i'm guessing it's passing me reference list, , actual list gets deleted function terminates, i'm not sure how resolve problem. appreciated!

here's code:

import os import tkinter tkinter import filedialog #import decimal decimal import *  def import_viscosity_data(size_of_header):     ### function imports viscosity data multiple files, skipping      ### header passed inof form shearrate '\t' viscosity , puts      ### array of form test_num[result_type[data]]] result type      ### 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 file(s):'))      test_num = []     result_type = []     data_1 = []     data_2 = []      file_name in file_path:          f = open(file_name)          ## skip header, consists of header_size lines         in range(header_size):             next(f)          lines = [line.strip() line in f]         f.close()           ## line, slice characters before tab, after tab         ## convert decimal, , append data list         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) 

here's sample data try on (any data in 2 columns tab in between):

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 

again, within program (running in ide, or if write small main() function), returns list of list of lists, , works fine. however, when import function in different file, returns none, without throwing errors. function name pops automatically in ide after import load_files, seems importing properly.

note *this secondary problem resolved. file load_files.py within directory called load_files. import statement changed from load_files import load_files , functions properly.*

today problem has gotten worse. now, can't functions first file recognized in second. simple set of code like:

#load_files.py def test_func():     print('test successful')  #test.py import load_files load_files.test_func() 

is throwing error:

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 in it's own folder (of same name) blank __init__.py file

note should add i'm using pyzo ide because want use scipy library curve fitting / optimization problems. can't functions import correctly today pyzo, no matter how simple. has else had problem?

the problem test.py file's import statement. @ first, confused issue having, in same directory test.py, load_files.py , directory called load_files contained load_files.py blank file called __init__.py.

the original script read

import load_files print(load_files.import_viscosity_data(7)) 

i eliminated load_files.py shared directory test.py. now, have test.py in parent directory, sub-directory called load_files contains load_files.py. new script reads:

from load_files import load_files print(load_files.import_viscosity_data(7)) 

now, list of lists passed in local space, statement

list_test = load_files.import_viscosity_data(7) 

works fine.

i couldn't things work correctly when had 2 .py files in same directory (e.g. test.py , load_files.py in same directory, no sub-directory). import statement of import load_files threw error module doesn't exist. anyway, works above code.

special martijn pieters feedback.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -