Realization Method of python Searching Path of Packet

  • 2021-07-22 10:52:25
  • OfStack

View the implementation of the path of python search package:

The path of the python search packet is stored under sys. path

Viewing method:

import sys

sys.path

Temporarily add python search method for packet path:

Method 1: (Enter python first)

import sys

sys. path. append ('Path')

(This method is only valid for the current python)

Method 2:

export PYTHONPATH = Path

(This works for all python)

Permanently write sys. path:

Method 1:

Add the. pth file under the existing sys. path search path, and the file content format is:

import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")

Method 2:

Add the following 1 line to the. bash_profile file under the home folder:

export PYTHONPATH = Path

Note that the values of sys. path are different after starting different python, for example:

python under boot conda is the value of sys. path


[
'', 
'/Users/zj_macbook/anaconda/lib/python27.zip', 
'/Users/zj_macbook/anaconda/lib/python2.7', 
'/Users/zj_macbook/anaconda/lib/python2.7/plat-darwin', 
'/Users/zj_macbook/anaconda/lib/python2.7/plat-mac', 
'/Users/zj_macbook/anaconda/lib/python2.7/plat-mac/lib-scriptpackages', 
'/Users/zj_macbook/anaconda/lib/python2.7/lib-tk', 
'/Users/zj_macbook/anaconda/lib/python2.7/lib-old', 
'/Users/zj_macbook/anaconda/lib/python2.7/lib-dynload', 
'/Users/zj_macbook/anaconda/lib/python2.7/site-packages', 
'/Users/zj_macbook/anaconda/lib/python2.7/site-packages/Sphinx-1.4.6-py2.7.egg', 
'/Users/zj_macbook/anaconda/lib/python2.7/site-packages/aeosa', 
'/Users/zj_macbook/anaconda/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg'
]

Start


/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python
 Adj. python Adj. sys.path Value of 
[
'', 
'/usr/local/lib/python2.7/site-packages/six-1.10.0-py2.7.egg', 
'/Library/Python/2.7/site-packages/twilio-6.3.dev0-py2.7.egg', 
'/Library/Python/2.7/site-packages/httplib2-0.9.2-py2.7.egg', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
'/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', 
'/Users/zj_macbook/Library/Python/2.7/lib/python/site-packages', 
'/usr/local/lib/python2.7/site-packages', 
'/usr/local/Cellar/numpy/1.12.0/libexec/nose/lib/python2.7/site-packages', 
'/usr/local/Cellar/protobuf/3.2.0/libexec/lib/python2.7/site-packages', 
'/Library/Python/2.7/site-packages', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC'
]

Related articles: