import
is not the same as including the content of the file as if you had typed it directly in place of the import
statement. You might think it works this way if you're coming from a C background, where the #include
preprocessor directive does this, but Python is different.
The import
statement in Python reads the content of the file being imported and evaluates it in its own separate context - so, in your example, the code in some_module.py
has no access to or knowledge of anything that exists in test.py
or any other file. It starts with a "blank slate", so to speak. If some_module.py
's code wants to access the os
module, you have to import it at the top of some_module.py
.
When a module is imported in Python, it becomes an object. That is, when you write
import some_module
one of the first things Python does is to create a new object of type module
to represent the module being imported. As the interpreter goes through the code in some_module.py
, it assigns any variables, functions, classes, etc. that are defined in that file to be attributes of this new module object. So in your example, the module object will have one attribute, testf
. When the code in the function testf
wants to access the variable os
, it looks in the function itself (local scope) and sees that os
is not defined there, so it then looks at the attributes of the module object which testf
belongs to (this is the "global" scope, although it's not truly global). In your example, it will not see os
there, so you get an error. If you add
import os
to some_module.py
, then that will create an attribute of the module under the name os
, and your code will find what it needs to.
You may also be interested in some other answers I've written that may help you understand Python's import
statement: