Python introduces the concept of multiple modules and packages

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.


  1. Create a new test_message folder
  2. In the directory, create two new files, and
  3. In send_ A send function is defined in the message file
def send(msg):
    print("Sending message:%s" % msg)
  1. In receive_ A receive function is defined in the message file
def receive():
    print("Receiving message......")
  1. In test_ Create a new one under the message folder__, 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
  1. Import HM directly from outside_ Message package
import test_message
test_message.send_message.send("Test sending information")
#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

  1. 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="",  # Author email
      url="nothing",  # homepage

For details of the parameters of the setup function, please refer to the official website:

  1. Building blocks
D:\project> python build

After execution, you will find a build folder in the directory, which contains some files about the module.

  1. Generate and publish compressed packages
D:\project> python 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.
Execute command:

python 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")

# D:\python\lib\site-packages\test_message\

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

Tags: Python Programming

Posted by i on Thu, 19 May 2022 05:24:41 +0300