简体   繁体   中英

How does importing a function from a module work in Python?

I have a module some_module.py which contains the following code:

def testf():
    print(os.listdir())

Now, in a file named test.py , I have this code:

import os
from some_module import testf

testf()

But executing test.py gives me NameError: name 'os' is not defined . I've already imported os in test.py , and testf is in the namespace of test.py . So why does this error occur?

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:

The name testf is in the namespace of test . The contents of the testf function are still in some_module , and don't have access to anything in test .

If you have code that needs a module, you need to import that module in the same file where that code is. Importing a module only imports it into the one file where you import it. (Multiple imports of the same module, in different files, won't incur a meaningful performance penalty; the actual loading of the module only happens once, and later imports of the same module just get a reference to the already-imported module.)

Importing a module adds its name as an attribute of the current scope. Since different modules have independent scopes, any code in some_module cannot use names in __main__ (the executed script) without having imported it first.

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