Python Search Path

Source: Internet
Author: User

Transferred from: http://www.jbxue.com/article/15324.html

In Python programming, the import of modules requires a process called "Path search".
Locate the mymodule.py file in the file system "predefined area" (If you import MyModule).
These predefined areas are just a collection of your Python search paths.

The default search path is specified at compile time or during installation. It can be modified in one or two places.
One is the PYTHONPATH environment variable that launches the shell or command line of Python. The content of the variable is a set of directory paths separated by colons.

If you want the interpreter to use this variable, be sure to set or modify the variable before starting the interpreter or executing a Python script.
After the interpreter is started, the search path can also be accessed, which is stored in the Sys.path variable of the SYS module.
However, it is not a colon-delimited string, but it contains a list of each independent path.

A sample of a Unix machine search path.
Note: The search path is generally different under different systems.

Copy CodeCode sample:>>> Sys.path
[‘‘,
'/usr/local/lib/python2.x/',
'/usr/local/lib/python2.x/plat-sunos5 ',
'/usr/local/lib/python2.x/lib-tk ',
'/usr/local/lib/python2.x/lib-dynload ',
'/usr/local/lib/python2.x/site-packages ',]

This is just a list, so you can modify it anytime, anywhere.

If you know what module you need to import, and its path is not in the search path, you just need to call the Append () method of the list, just like this:
Sys.path.append ('/home/wesc/py/lib ')
Once the modifications are complete, you can load your own modules.

Python Search Path

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.