[英]After importing a module (func1.py) into the test.py file, getting nameError: name ‘pd’ is not defined
import sys
sys.path.append('/Users/name/Documents/pythontemp/functemp/')
In the test.py file:
import pandas as pd
for file in os.listdir('/Users/name/Documents/pythontemp/functemp/'):
if 'func' not in file:
continue
fileN=file.split('.')[0]
print(fileN)
Class=__import__(fileN)
func=getattr(Class,fileN)
para=func.__code__.co_varnames
print(para)
if 'n1' not in para:
idx=func.__code__.co_argcount
else:
idx=para.index('n1')
print(idx)
paras=[eval(x) for x in para[:(idx)]]
#print(paras)
dict_=dict(zip(para[:idx],paras))
#print(dict_)
factor=func(**dict_)
display(factor)
The nameError is:
nameError是:
func2 ('a', 'b', 'c', 'result') 3
NameError
Traceback (most recent call last)
<ipython-input-9-4b45b006dea5> in <module>
22 #print(dict_)
23
---> 24 factor=func(**dict_)
25 display(factor)
~/Documents/pythontemp/functemp/func2.py in func2(a, b, c)
1 def func2(a,b,c):
----> 2 a=pd.DataFrame(a)
3 result=a+b-c
4 return result
NameError: name 'pd' is not defined
The func2.py file is: func2.py文件是:
def func2(a,b,c):
a=pd.DataFrame(a)
result=a+b-c
return result
I have lots of py file like func2.py
(eg. func1.py
, func3.py
...) in the same file folder. 我在同一文件夹中有很多像
func2.py
(例如func1.py
, func3.py
...)这样的py文件。 In the test.py
, I have imported pd
, but func2.py
not. 在
test.py
,我imported pd
,但func2.py
没有。
So how to solve it? 那怎么解决呢?
The NameError
in func2.py
is because that file does not know what pd
is because you have not imported it in that file. func2.py
的NameError
是因为该文件不知道pd
是什么,因为您没有在该文件中导入它。 Note that you have correctly imported pandas in the test.py
file. 请注意,您已在
test.py
文件中正确导入pandas。 Add the following to the top each of the func*.py
files that have pd: 将以下内容添加到具有pd的每个
func*.py
文件的顶部:
import pandas as pd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.