Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use module and package in python

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use module and package in python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use module and package in python" can help you solve the problem.

Continue to nest a package under package, and the project directory is as follows

Pkg1---__init__.py---mod1.py-pkg2-__init__.py-mod2.pyimport pkg1.pkg2.mod2

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

Import pkg1import pkg2import mod2

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

Import pkg1.pkg2.mod2pkg1.pkg2.mod2.say_hi ()

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

Print ('pkg1' in sys.modules) # Trueprint (' pkg1.pkg2' in sys.modules) # Trueprint ('pkg1.pkg2.mod2' in sys.modules) # True

Quickly access the properties of the mod2 module object by giving the import module an alias. Python automatically executes the package through which the mod2 can be accessed, and it is not difficult to find out from the output that the package has been imported and added to the sys.modules cache.

Import pkg1.pkg2.mod2 as mod2mod2.say_hi () pkg1---__init__.py---mod1.py-pkg2-__init__.py-mod2.pymain.py

Under pkg1 package _ _ init__.py

Import pkg1.pkg2.mod2print ("import pkg1")

Here we import mod2 module into the _ _ init__.py file under pkg1 package is the absolute path pkg1.pkg2.mod2. It means that we execute the _ _ init__.py file outside the pkg1, so the path also contains pkg1. For this project, main.py is the entry file. To execute this project, we only need to execute the main.py file under the folder. Pkg1 and main.py are at the same directory level, and any submodule (sub-module) is

Import pkg1.pkg2.mod2 as mod2mod2.say_hi ()

To explain further, let's create another project, which devolves two

Main.pymy_mod.pyimport socketx=2def say_hi (): print ("say hi from my mod")

The code in main.py is as follows

Import my_mod as modprint (mod.x) print (mod.socket.gethostname ())

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

Server---app.py

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

Print ("running app")

In python, you can search for the specified file in the directory and execute it. The python interpreter provides the-m parameter that allows you to specify the module name to execute a python module file, so you can run the app.py file under the server folder with the following statement.

Python-m server.app

Python will search the sys.path path for `server/app.py

We can also rename the app.py under server to _ _ main__.py as follows

Server---__main__.py

This makes it possible to execute the _ _ main__.py file under server package directly through the python-m server statement. Of course, we can create a _ _ init__.py and _ _ main__.py file in server package at the same time.

This is the end of the introduction about "how to use module and package in python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report