Python 模块导入不起作用



我收到以下代码的错误:

from os import getcwd
os.getcwd()
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
os.getcwd()
NameError: name 'os' is not defined

有谁知道为什么以这种方式导入不起作用?

有两种可能性。第一种是导入模块并在每次调用函数时命名模块:

import os
print os.getcwd()

或者可以直接导入模块的方法,调用模块时不必命名模块:

from os import getcwd
print getcwd()
from os import getcwd
print getcwd()

当您只导入getcwd而不导入os这如何工作os.getcwd

最新更新