Python import custom Packages or modules

Source: Internet
Author: User

In general, we will write our own Python module and Python's own modules stored separately to achieve easy maintenance purposes.

The Python runtime environment is a sys.path list when looking for a module, and if we want to add a custom module to the runtime, there are three main ways to do this:

1. Add a new path to the Sys.path list (only works for the current Python runtime that executes the Sys.path.append command, which does not work for the other runtime, i.e. "disposable").

>>> Import sys

>>> Sys.path

>>> sys.path.append ('/home/xxx/yyy ') #目录 /home/xxx/yyy contains the packages or modules you need

Set the PYTHONPATH environment variable (permanently added):

2. Copy the package or module to a directory in the Sys.path list (Sys.path view) (such as the/home/test/lib/python2.7/site-packages/directory).

3. the simplest approach is to use the. pth file to implement. as Python iterates through the existing library file directory (specified in Sys.path), if a. pth file is seen, the path that is recorded in the file is added to the Sys.path setting so that the. pth file says that the specified library can be found in the Python runtime environment.

$CD /home/test/lib/python2.7/site-packages/

$touch test.pth

$vim test.pth

$ Add a single line of packages or modules in the same directory (for example:/home/test/somepackage/)

the contents of the Test.pth file are:

/home/test/somepackage/

Python import custom Packages or modules

Related Article

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.