Python global name "os" is no longer defined after module call

I am a lover of rank in python, I am sure that there is a simple solution for this. I am trying to call a module that uses the os.path.exists function. If I write something like below, the code works fine.

import os if os.path.exists('text.txt'): print 'yes' 

However, I get this error: "NameError: the global name" os "is not defined" when I call the module as shown below -

 import os import modutest modutest.test() 

with this module (I called it modutest.py)

 def test(): if os.path.exists('text.txt'): print 'yes' 
+4
source share
1 answer

Just import the os module into modutest.py, as shown below:

 import os def test(): if os.path.exists('text.txt'): print 'yes' 

modutest.py is a completely different file, so you need to import the os module.

+5
source

Source: https://habr.com/ru/post/1482829/


All Articles