简体   繁体   中英

Python module import not working

I am getting an error with the following code:

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

Anyone know why importing this way is not working?

There are two possibilities. The first one is to import the module and name the module every time calling the function:

import os
print os.getcwd()

Or you can import the method of the module directly and don't have to name the module when calling it:

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

When you have only imported getcwd and not os how can this work os.getcwd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM