1

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?

sheldonzy
  • 5,505
  • 9
  • 48
  • 86
West
  • 2,350
  • 5
  • 31
  • 67

2 Answers2

2

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()
Robin
  • 303
  • 1
  • 4
  • 16
  • Thanks that works. I guess I have to omit the module name whenever I use a function imported that way right? – West Oct 17 '17 at 09:46
1
from os import getcwd
print getcwd()

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

vks
  • 67,027
  • 10
  • 91
  • 124