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