Make writing a habit together! This is the third day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.
A package is an efficient organization in Python. A module can be a file. You can import a single module file through import, while a package is imported as a directory. We’ll also look at how multi-level nesting is imported later.
>>> import collections,socket
>>> print(collections.__path__)
['/ anaconda3 / envs/py38 / lib/python3.8 / collections']
>>> print(socket.__path__)
Traceback (most recent call last):
File "<stdin>", line 1.in <module>
AttributeError: module 'socket' has no attribute '__path__'
Copy the code
Python provides annotation libraries, such as Collections and sockets, where collections is a package meaning it’s a directory, and sockets are a module in A Python file. For sockets, we did the same with importing modules. Package, unlike Module, has a __path__ attribute, and we can ask __path__ to find the path to the Python file. But this attribute does not exist for the Module.
In Python, there are two types of package: regular package and namespace package.
regular package
Let’s look at the regular package structure
main.py
pkg1
---__init__.py
Copy the code
Under the project, we create a folder named kpg1 with a file named __init__.py under it, so that the package is regular Package and the folder name is a package name. By importing package, we can import the package name directly. Add the following statement to the __init__.py file.
print("importing pk1")
def hi_say() :
print("pkg1 say hi")
Copy the code
In main.py we import the package, import pkg1, and Python uses finder to locate the package location, PathFinder locates the package by searching the path in sys.path. Do you remember? Path The first path is our current directory. This is why Python can locate pkg1, because when we import package Python automatically executes the __init__.py file under package. When you import package, the Module compiles the code object, which we can do by pkg1.hi_say().
print(pkg1.__path__)
Copy the code
Pkg1’s absolute directory path is called by pkg1’s __path__.
print(pkg1.__file__)
Copy the code
__file__ corresponds to the absolute path to __init__.py.
print(pkg1.__package__)
Copy the code
The name of the package can also be obtained from the __package__ attribute of the package, and the name of the Module can also be obtained from __name__.
Next we will make it more difficult by creating a new mod1.py file under pkg1
main.py
pkg1
---__init__.py
---mod1.py
Copy the code
Just set py files
print("importing pk1")
Copy the code
The mod1.py file outputs a message saying “import mod1” and defines the say_hi function. What we want to do is import the module and execute the say_hi method.
print("importing mod1")
def say_hi():
print("pkg1 say hi")
Copy the code
import sys
import pkg1
print('pkg1' in globals())#True
print('pkg1' in sys.modules)#True
Copy the code
We import PKg1 to create a reference to the module object and add it to the global variable, as well as to the sys.modules cache.
Then when we access mod1 module directly through pkg1.mod1, the following error is thrown, telling the user that this attribute does not exist
AttributeError: module 'pkg1' has no attribute 'mod1'
Copy the code
That is, simply importing the package does not import the modules under it, which is why the above error occurs. If we want to import mod1 from PKg1, we need to import pkg1.mod1 in the correct way
import sys
import pkg1.mod1
Copy the code
Note that the correct way to import mod1 is to import pkg1.mod1. Python executes __init__. That is, Python will import Package PKg1 first and then module1 in that order.
importing pk1
importing mod1
Copy the code
The say_hi() method in module mod1 can be accessed by:
pkg1.mod1.say_hi()
Copy the code
print('pkg1' in sys.modules)#True
print('pkg1.mod1' in sys.modules)#True
print('pkg1' in globals())#True
print('pkg1.mod1' in globals())#False
Copy the code
Modules cache pkg1 and pkg1.mod1 exist in sys.modules cache, and it is not hard to see that only pkg1 is in the global variable, we can only mod1 through pkg1 attributes.
To summarize, we need to call a module from a package, first access the package, execute __init__.py from the package, and then execute the module code, Save pkg1 and pkg1.mod1 in sys.modules to the memory addresses where the two Module objects are stored, respectively.