How to add custom modules in Python

Generally speaking, we will store the Python modules we write and the modules that come with python separately to achieve the purpose of easy maintenance. So how to add custom modules in Python?

Before answering this question, we must first clarify two points:

  1. Strictly distinguish between packages and folders. The definition of a package is the folder containing init.py. If there is no init.py, then it is a normal folder.

  2. Module import writing, pay attention to only the package path, not the folder path.

The Python runtime environment traverses the sys.path list when searching for library files. If we want to register a new class library in the runtime environment, there are two main methods:

  1. Add a new path to the sys.path list.

  2. Copy the library file to a directory in the sys.path list (such as the site-packages directory).

We can view sys.path by running the code

import sys
print sys.path

operation result

[ ‘/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old’, ‘/System/Library/Frameworks/Python.
framework/Versions/2.7/lib/python2.7/lib-dynload’, ‘/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/
lib/python/PyObjC’, ‘/Library/Python/2.7/site-packages’]

The first of these two methods is simpler and has the least impact on the environment.

Let's take a look at the specific operation of the first method:

Create a new pythontab.pth in the site-package folder of the python installation directory. The above site-package path is: /Library/Python/2.7/site-packages, and the content of the file is: the folder path where the package to be imported is located.

In this way, in the process of traversing the known library file directory, if Python sees a .pth file, it will add the path recorded in the file to the sys.path setting, so that the .pth file says the specified package can also After being successfully found by the Python runtime environment, we can introduce custom modules like built-in modules.

If the default sys.path does not contain the path of your own module or package, we can also use the sys.path.apend method to dynamically add the package path.

Knowledge point expansion:

Principles of adding custom modules in Python:

  1. Strictly distinguish between packages and folders. The definition of a package is the folder containing init.py. If there is no init.py, then it is a normal folder.

  2. Import the package. Create a new xxx.pth in the site-package folder of the python installation directory, the content is the folder path of the package to be imported.

  3. Import the module. It is the general way of importing modules. Note that only the package path is required, not the folder path.

The above is the details of how to add custom modules in Python. For more information about adding custom modules in Python, please pay attention to other related articles on ZaLou.Cn!

Recommended Posts

How to add custom modules in Python
How to introduce third-party modules in Python
How to install third-party modules in Python
How to custom catch errors in python
How to view installed modules in python
How to add background music in python
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to add swap partition in Ubuntu
How to use and and or in Python
How to represent null values in python
How to write win programs in python
How to write try statement in python
How to add users to Sudoers in Ubuntu
How to define private attributes in Python
How to understand global variables in Python
How to open python in different systems
How to sort a dictionary in python
How to represent relative path in python
How to use the round function in python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to use code running assistant in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to install php7.3 in centos8 custom directory
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
Introduction to Python related modules
How to understand the introduction of packages in Python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
How to create a Python virtual environment in Ubuntu 14.04
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
python how to view webpage code
How to use hanlp in ubuntu
How to use python thread pool
How to install PHP7.4 in CentOS
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install HDP2.6 in Centos7.2
How to solve python dict garbled
How to install mysql in Ubuntu 14.04