Categories
import python python-3.x

Using modules imported from another import

I’m cleaning up a project that was refactored into smaller .py files. I noticed that a lot of modules are being imported again and again in various files. Some statements are in files that import another which has the same import statement used by the importing file. For example:

main.py

import alt
print (os.getcwd())

alt.py

import os

The print(os.getcwd()) throws a NameError: name 'os' is not defined. Shouldn’t os be part of sys.modules when the import statement is executed in alt.py?

Is it possible to use a module imported by another module that was imported first?

They are available in the following namespace:

import alt
print (alt.os.getcwd())