How to add a custom module in Python

Source: Internet
Author: User
In general, we store the Python module we write separately from the python module to facilitate maintenance. So how to add custom modules in Python? In general, we will separate the Python module we write from the module that comes with python to facilitate maintenance. So how to add custom modules in Python?

Before answering this question, we must clarify two points:

1. strictly differentiate packages and folders. The package is defined as a folder containing init. py. If there is no init. py, it is a normal folder.

2. write the module import method. Note that the package path is not a folder path.


The Python runtime environment traverses the sys. path list when searching for library files. 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 the Directory (such as the site-packages directory) in the sys. path list ).


Run the code to view sys. path.

import sysprint sys.path

Running 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 the two methods is relatively simple and has the least impact on the environment.

Next, let's take a look at the specific operation of the first method:


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

In this way, when Python traverses the known library file directory, if you see. in the pth file, the path recorded in the file will be added to sys. path settings. the pth file indicates that the specified package can be found smoothly in the Python runtime environment, and we can introduce the custom module just like using the built-in module.

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

The preceding section describes how to add a custom module to Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.