2017-10-17 110 views
1

我得到一個錯誤與下面的代碼: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 

誰知道爲什麼進口這種方式是不工作?

回答

1
from os import getcwd 
print getcwd() 

當你只有進口getcwd而不是os怎麼能這樣工作os.getcwd

+0

現在我明白了。我只是新的python,所以仍然在努力與幾個基礎 – West

+0

@West如果你'進口操作系統',那麼你可以做'.'部分 – vks

+0

酷感謝記住, – West

2

有兩種可能性。第一個是導入模塊,每次命名模塊調用該函數:

import os 
print os.getcwd() 

或者,您可以直接導入模塊的方法,並沒有對罵時,它的模塊:

from os import getcwd 
print getcwd() 
+0

感謝的作品。我想我必須忽略模塊名稱,每當我使用一個正確的方式導入的功能? – West