Python exploration: modifying the Python search path and exploring the python search path
When Python executes the import Statement, it searches for the Python module and extension module in some paths. You can view these paths through sys. path, for example:
>>> import sys >>> sys.path ['', '/usr/lib/python2.7/site-packages/demo_nova_hooks-5-py2.7.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
The empty string indicates the current working directory.
When installing third-party modules, if they are not installed in a standard way, you must add the installation paths of these modules to sys to be able to reference these modules. there are several methods in path:
1: the simplest method is to add a path configuration file under a directory in sys. path. The most common method is... /Site-package/directory. The path configuration file extension is ". pth". Each row contains a separate path, which is added to the sys. path list (verified ).". The path in pth can be either an absolute or relative path. If it is a relative path, it is relative to the path containing the ". pth" file.
2: Modify the site. py file in the Python standard library and edit sys. path. Unless the-S switch option is used, site. py is automatically introduced (executed) when the Python interpreter is loaded. It loads the packages and modules in site-packages to the python sys. path. Therefore, you can edit site. py and add the following two lines:
import sys sys.path.append('/xxx/xxxxx/')
3: There are two environment variables to edit sys. path. PYTHONHOME will change the values of prefix and exec_prefix. By default, both prefix and exec_prefix are/usr/local. The library search path is prefix/lib/pythonversion and exec_prefix/lib/pythonversion.
If you set PYTHONHOME as a separate path, the values of prefix and exec_prefix will be replaced. If you need different values of prefix and exec_prefix, set PYTHONHOME to "prefix: exec_prefix ". For example, if you set PYTHONHOME to "/www/python", the sys. path will change
['', '/Www/python/lib/pythonX. Y/', '/www/python/lib/pythonX. Y/plat-linux2',...].
Note: After you modify the PYTHONHOME, the system may not be able to start the python interpreter because the site module cannot be found. Therefore, do not modify this variable unless necessary.
[root@localhost ~]# python ImportError: No module named site
A series of paths specified by the PYTHONPATH environment variable are added to sys. path. For example, if PYTHONPATH is set to "/www/python:/opt/py", then sys. the path will start with ['/www/python','/opt/py'].. Note that these paths must exist because the site module will delete non-existing paths.
Summary
For Python path modification, we will first introduce it here. If you have any good articles about Python, we will share them with you as soon as possible. If you have any questions, please feel free to leave a message. You are welcome to exchange your reference. Several Python articles on this site will be shared with you: differences between copy and deepcopy in python, detailed descriptions of the python implementation interface (itchat), and machine learning exploration based on Python and Scikit-Learn.
I have been late for many years on the way to pursue my dream. Now, it's time to get on the bus ..