In actual development, a large project often needs to use hundreds of Python modules. If these modules are stacked together, it is bound to be difficult to manage.
Moreover, using modules can effectively avoid conflicts caused by duplicate variable names or function names, but what if the module names are repeated?
Therefore, Python proposes the concept of Package.
1, Concept and basic use of package
What is a bag? Simply understand, a package is a folder, but there must be a file named "_init_. Py" under the folder.
One must be created under the directory of each package__ init__. The function of Py module is to write some code as an empty package.
The package name is named in the same way as the variable name, 'lowercase' + ''.
Use 'import package name' to import all modules in the package at one time.
- Create a new test_message folder
- In the directory, create two new files, send_message.py and receive_message.py
- In send_ A send function is defined in the message file
def send(msg): print("Sending message:%s" % msg)
- In receive_ A receive function is defined in the message file
def receive(): print("Receiving message......")
- In test_ Create a new one under the message folder__ init__.py, and specify the list of modules provided to the outside world in this file
# Import module list from current directory from . import send_message from . import receive_message
- Import HM directly from outside_ Message package
import test_message test_message.send_message.send("Test sending information") test_message.receive_message.receive() #Sending information: Test sending information #Receiving message
2, Release module
If you want to share your module with others, you can follow the steps below.
1. Steps for making and publishing compressed packages
- Create setup py
from distutils.core import setup setup(name="test_message", # Package name version="1.0", # edition description="Sending and receiving message module", # Description information long_description="Complete sending and receiving message module", # Complete description information author="His nickname is Xiao Ming", # author author_email="email@example.com", # Author email url="nothing", # homepage py_modules=["hm_message.send_message", "hm_message.receive_message"])
For details of the parameters of the setup function, please refer to the official website:
- Building blocks
D:\project> python setup.py build
After execution, you will find a build folder in the directory, which contains some files about the module.
- Generate and publish compressed packages
D:\project> python setup.py sdist
After execution, it is found that there is an additional folder dist in the current directory, which contains a compressed file test_message-1.0.tar.gz.
Note here: the version of the module you want to make will be executed with the interpreter of that version!
2. Installation module
First, unzip the compressed file and enter the folder.
python setup.py install
After installation, we can test whether the package can be used normally.
Remember to delete the original package and the downloaded and unzipped package. In this way, it can be tested that the package is installed in the system directory, not the development directory.
import test_message test_message.send_message.send("Test sending information") test_message.receive_message.receive() print(test_message.__file__) # D:\python\lib\site-packages\test_message\__init__.py
3. Uninstall module
According to the package__ file__ Get the information directly from the installation directory and delete the directory of the installation module.
3, Installing third-party modules
Third party modules usually refer to Python packages / modules developed by well-known third-party teams and widely used by programmers.
When installing third-party modules, we usually use pip tools.
pip is a modern and general Python package management tool, which provides the functions of finding, downloading, installing and uninstalling Python packages.
Let's download the pygame module.
pygame is a very mature game development module.
# install pip install pygame # uninstall pip uninstall pygame