How much do you know about modules and packages in python?

Get into the habit of writing together! This is the 4th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .

We continued the last sharing, this time we continue to nest a package under the package, the project directory is as follows

pkg1
---__init__.py
---mod1.py
------pkg2
---------__init__.py
---------mod2.py
复制代码
import pkg1.pkg2.mod2
复制代码

We try to import mod2, this time we will import pkg1 first, then pkg2 and finally mod2

import pkg1
import pkg2
import mod2
复制代码

From the output, we see that mod1 is not imported automatically.

import pkg1.pkg2.mod2

pkg1.pkg2.mod2.say_hi()
复制代码

We pkg1.pkg2.mod2.say_hi()access the say_hi function in mod2 by.

print('pkg1' in sys.modules)#True
print('pkg1.pkg2' in sys.modules)#True
print('pkg1.pkg2.mod2' in sys.modules)#True
复制代码

Quickly access the properties of the mod2 module object by aliasing the imported module. Python will automatically execute packages that can access mod2. It is not difficult to find that these packages have been imported and added to the sys.modules cache through the output.

import pkg1.pkg2.mod2 as mod2

mod2.say_hi()
复制代码
pkg1
---__init__.py
---mod1.py
------pkg2
---------__init__.py
---------mod2.py
main.py
复制代码

under the pkg1 package__init__.py

import pkg1.pkg2.mod2
print("import pkg1")
复制代码

The __init__.pyfile is the absolute path pkg1.pkg2.mod2. It means that our execution __init__.pyfile is executed outside pkg1, so pkg1 is also included in the path. For this project, main.py is the entry file. To execute this project, we only need to execute the main.py file in the folder. pkg1 and main.py are at the same directory level, any sub-module is

import pkg1.pkg2.mod2 as mod2

mod2.say_hi()
复制代码

In order to explain our further explanation, let's create another project, and the project is divided into two

main.py
my_mod.py
复制代码
import socket
x=2
def say_hi():
    print("say hi from my mod")
复制代码

The code in main.py is as follows

import my_mod as mod

print(mod.x)
print(mod.socket.gethostname())
复制代码

So we know that we import my_mod in main.py as, and then import socket module in my_mod file, which can be imported through mod.socket.

server
---app.py
复制代码

Here we only add one statement to app.py, which is output in the terminal

print("running app")
复制代码

In python, you can search for the specified file in the directory and execute it. The python interpreter provides -mparameters that allow us to specify the module name to execute a python module file, so you can run the app.py file in the server folder through the following statement.

python -m server.app
复制代码

python will search for `server/app.py in the sys.pathpath

We can also rename app.py under server as __main__.pyfollows

server
---__main__.py
复制代码

In this way, the files under the server package can be executed directly through the python -m serverstatement . __main__.pyOf course we can create one __init__.pyand __main__.pytwo files in the server package at the same time.

Guess you like

Origin juejin.im/post/7082654440653586439