需要在某个地方导入声明:import import_module
class local_class():
def local_function():
action = raw_input()
if action = 'fow':
import_module.import_function
else:
print 'null'
或者
^{pr2}$
但是请注意,一旦导入,模块将由python内部存储,这样即使您不能在另一个范围内访问它,如果您再次导入模块,您将得到相同的实例。例如:>>> def func():
import shutil
# Store a new attribute in the module object
shutil.test = 5
print(shutil.test)
>>> func()
5
>>> shutil.test
Traceback (most recent call last):
File "", line 1, in
shutil.test
NameError: name 'shutil' is not defined
>>> import shutil
>>> shutil.test # The attribute exists because we get the same module object
5
>>>