Python模块中的特殊属性
Le Thu 09 October 2025
Like most other programming languages, in Python the codebase is also organized into modules. The modules in Python are created by the import system, either using an import statement or using importlib.import_module() or directly using the import() function call.
The module object has a namespace associated with it which is a dictionary dict. Attribute reference of module object is translated to a lookup into the dictionary.
Module object has some attributes that gets created and filled with data while the module object itself is created. In this article we will discuss about all the attributes that Python module objects has and what they are for.
module.name The name attribute is the fully qualified name of the module. This is used to uniquely identify the module in the import system. When you directly execute a script, its name attribute is set to main.
module1.py
def function(): print("function")
module2.py
import module1
print(module1.name)
module1
print(name)
main
In the above code, when we access name of module1 from inside the module2 we get “module1” as value. And when we directly access the name attribute of currently executing module we get main.
module.spec The import machinery uses a variety of imformation about the modules. Those information are the specs of the modules. The purpose of the module spec is to keep the import related information on per module basis. The module spec is used to transfer state information between different componenets of import system. When a Python script is executed directly its spec is set to None.
module1.py
def function(): print("function")
module2.py
import module1
print(module1.spec)
ModuleSpec(name='exercise', loader=<_frozen_importlib_external.SourceFileLoader object at 0x10682e420>, origin='/Users/admin/Documents/module1.py')
print(spec)
None
module.package The package attribute of a module is used to indicate the package where the module belongs to. If the module is a top level script that gets run directly then the package attribute is set to empty string (’’) or None. This attribute helps Python resolve relative imports correctly.
Consider the following package structure.
mypackage/ init.py module1.py subpackage/ init.py module2.py script.py If we print the package attribute inside module2.py, by running it as part of imported package would print something like “mypackage.subpackage”.
module2.py
print(package)
script.py
from mypackage.subpackage import module2
OUTPUT:
mypackage.subpackage
However, if we run module2.py directly using python module2.py, it will print empty string (’’) or None.
module.loader The loader object that is used to load the module by the import system. This attribute is usefull to get access to the load specific functionality of a module. Python’s importlib module uses this for advanced module mechanism.
import script
print(script.loader)
<_frozen_importlib_external.SourceFileLoader object at 0x1092fe420>
module.path It is a sequence of string that indicates where the packages submodules will be found. If a module has path attribute that means it is a package. Non-package modules should not have this attribute.
When we have the package structure mentioned above the path attribute will output like following.
script.py
import mypackage
print(mypackage.path)
['/Users/admin/Documents/mypackage']
module.file This attribute contains a string value that points to the location from where the module was loaded. It is only available if the module is loaded from a file based source. Built-in module don’t have file attribute and when a module is loaded non-standard way, thie attribute may absent.
import mypackage
print(mypackage.file)
/Users/admin/Documents/mypackage/init.py
module.cached This attribute is a string path of the compiled version of the code (.pyc) inside the pycache directory. Usually if file attribute is set then cached will also be set, but there are some cases where cached has value where as file may not have.
import mypackage
print(mypackage.cached)
/Users/admin/Documents/mypackage/pycache/init.cpython-312.pyc
module.doc This attribute is set to the documentation string of the module if present otherwise it is set to None.
"""This is documentation string"""
print(doc)
This is documentation string
module.annotations It is a dictionary that contains variable annotations collected during module body execution. If no annotations exists, it defaults to empty dictionary {}. It doesn’t include annotations of local variables inside functions.
script.py
count: int = 0
print(annotations)
{'count': }
module.dict This is a namespace dictionary that contains all the module level variables, classes, functions and objects. It is mutable, so you can modify it dynamically. You can not access dict as a global variable within a module but can access it as an attribute of module objects.
import mymodule
print(mymodule.dict) OUTPUT
{
'name': 'mymodule',
'doc': None,
'package': None,
'loader': <...>,
'spec': <...>,
'x': 10,
'y': 'hello',
'greet':
参考资料
- https://docs.python.org/3/reference/datamodel.html#callable-types
- https://docs.python.org/3/reference/import.html#importsystem