python搜索包的路径的实现方法
查看python搜索包的路径的实现方法:
python搜索包的路径存储在sys.path下
查看方法:
import sys
sys.path
临时添加python搜索包路径的方法:
方法1:(先进入python)
import sys
sys.path.append(‘路径')
(这种方式仅对当前python有效)
方法2:
export PYTHONPATH=路径
(这种方式对所有的python有效)
永久写入sys.path的方法:
方法1:
在已有的sys.path搜索路径下添加 .pth文件,文件内容格式为:
import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")
方法2:
在home文件夹下的.bash_profile文件中添加下面一行:
export PYTHONPATH=路径
注意,启动不同的python之后sys.path的值是不同的,例如:
启动conda下的python是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' ]
启动
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python 的python的sys.path的值 [ '', '/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' ]
以上这篇python搜索包的路径的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。
赞 (0)