简体   繁体   English

Python何时/如何使用PYTHONPATH

[英]When/how does Python use PYTHONPATH

I'm having some trouble understanding how Python uses the PYTHONPATH environment variable. 我在理解Python如何使用PYTHONPATH环境变量时遇到了一些麻烦。 According to the documentation, the import search path ( sys.path ) is "Initialized from the environment variable PYTHONPATH , plus an installation-dependent default." 根据文档,导入搜索路径( sys.path )是“从环境变量PYTHONPATH初始化,再加上与安装有关的默认值。”

In a Windows command box, I started Python (v.2.7.6) and printed the value of sys.path . 在Windows命令框中,我启动了Python(v.2.7.6),并打印了sys.path的值。 I got a list of pathnames, the "installation-dependent default." 我得到了一个路径名列表,即“依赖于安装的默认值”。

Then I quit Python, set PYTHONPATH to .;./lib; 然后我退出Python,将PYTHONPATH设置为.;./lib; , restarted Python, and printed os.environ['PYTHONPATH']. ,重新启动Python,并打印os.environ['PYTHONPATH']. I got .;./lib; 我得到了.;./lib; as expected. 如预期的那样。 Then I printed sys.path . 然后我打印了sys.path I think it should have been the installation-dependent default with .;./lib; 我认为应该是.;./lib;依赖于安装的默认值.;./lib; added to the start or the end. 添加到开头或结尾。 Instead it was the installation-dependent default alone, as if PYTHONPATH were empty. 相反,它只是依赖于安装的默认值,就像PYTHONPATH为空一样。 What am I missing here? 我在这里想念什么?

It always uses PYTHONPATH . 它始终使用PYTHONPATH What happened is probably that you quit python, but didn't quit your console/command shell. 发生的情况可能是您退出了python,但没有退出控制台/命令外壳。 For that shell, the environment that was set when the shell was started still applies, and hence, there's no PYTHONPATH set. 对于该外壳,启动外壳时设置的环境仍然适用,因此没有设置PYTHONPATH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM