简体   繁体   English

将模块(func1.py)导入test.py文件后,未定义nameError:name'pd'

[英]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.pyfunc3.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.pyNameError是因为该文件不知道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.

相关问题 获取NameError:名称'bot_token'未定义py.test - Getting NameError: name 'bot_token' is not defined py.test 当您只是从其他地方(例如 main.py)导入一段 test.py 时,为什么要运行 python 文件(例如 test.py)的其他代码行? - Why do you run other lines of codes of a python file(say test.py) when you are just importing a piece of test.py from somewhere else (say main.py)? 试图在文件上运行 unittest 模块并且... ModuleNotFoundError: No module named 'test.py' - Trying to run unittest module on file and… ModuleNotFoundError: No module named 'test.py' NameError: name ' ' not defined in.py/.kv - NameError: name ' ' not defined in .py/.kv ImportError运行Test.py - ImportError Running Test.py NameError:全局名称未使用另一个 .py 文件中定义的全局变量定义 - NameError: global name is not defined with a global variable defined in another .py file 如何解决此错误文件“test.py”,第 5 行,在<module> for _ in range(b[0]): TypeError: 'int' object 不可订阅</module> - how to resolve this error File "test.py", line 5, in <module> for _ in range(b[0]): TypeError: 'int' object is not subscriptable &#39;%%file test.py&#39; 在 python 中是什么意思? - what does '%%file test.py' mean in python? ArgParse错误。 但在Test.py文件中工作 - ArgParse ERROR. but working in Test.py file NameError:名称&#39;func&#39;未定义[运行&#39;Map(<lambda at testing.py:273> )-ptransform-38&#39;] - NameError: name 'func' is not defined [while running 'Map(<lambda at testing.py:273>)-ptransform-38']
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM