【发布时间】:2018-05-10 08:49:31
【问题描述】:
我在从文件中导入一个函数并在另一个函数中执行它时遇到问题。我无法将变量 'efgh' 传递给 fun_a()。
#cat fun_a.py
def fun_a(abcd):
print '%s - %s'%(abcd, efgh)
if __name__ == "__main__":
pass
#python
Python 2.7.5 (default, Oct 11 2015, 17:47:16) [GCC 4.8.3 20140911 (Red Hat
4.8.3-9)] on linux2 Type "help", "copyright", "credits" or "license" for
more information.
>>> from fun_a import *
>>> efgh = 'test2'
>>> fun_a('test1')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "fun_a.py", line 2, in fun_a
print '%s - %s'%(abcd, efgh)
NameError: global name 'efgh' is not defined
我希望结果是“test1 - test2”。
【问题讨论】:
标签: python function file import