PYTHONPATH
,我在Mac上使用bash,如果有关的话。这就是我做的事情:
open ~/.bash_profile
export PYTHONPATH="${PYTHONPATH}:/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages"
并保存source ~/.bash_profile
- 当我在Python IDE中运行
sys.path
时,预期的目录仍然没有显示出来。 - 当我在终端中启动Python并运行
sys.path
时,该目录显示出来了,但是所有其他目录与之前一步得到的不匹配。
sys.path
得到的内容。找不到预期的目录。
这是我从终端运行sys.path ['', '/Applications/Spyder-Py2.app/Contents/Resources', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python27.zip', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-darwin', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-mac', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-mac/lib-scriptpackages', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-tk', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-old', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-dynload', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/site-packages.zip', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/site-packages', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/IPython/extensions', '/Users/Zhengnan/.ipython']
sys.path
得到的内容。预期目录是列表中的第三个元素。``` sys.path ['', '/Users/Zhengnan', '/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages', '/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/Extras/lib/python', '/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', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
我想提一下,我想要将自定义目录添加到PYTHONPATH中的原因是每次我进行pip安装包时,它们都会被安装在/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages中,我不想在每次运行脚本时都进行sys.path.append操作。请给予建议。谢谢。 ```