简体   繁体   English

设置虚拟环境时,sys.path 的值在 windows 和 linux 之间有所不同

[英]Value of sys.path differs between windows and linux when setting up a virtual environment

When creating a virtual environment via python -m venv.venv and then activating this environment I notice that on Windows the root directory of the environment is part of the import path as reported by sys.path but for linux it's not.通过python -m venv.venv创建虚拟环境然后激活此环境时,我注意到在 Windows 上,环境的根目录是sys.path报告的导入路径的一部分,但对于 ZE206A54E97690CCE50CC872Z70EE 它不是。

Python version: 3.8.2 Python 版本: 3.8.2

Example:例子:

python -m venv .venv

On Windows:在 Windows 上:

>>> import sys
>>> sys.path
['', 'c:\\temp\\.venv\\Scripts\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib', 'C:\\Python36', 'c:\\temp\\.venv', 'c:\\temp\\.venv\\lib\\site-packages']
>>> sys.prefix
'c:\\temp\\.venv'

Notice the entry c:\\temp\\.venv in the path.请注意路径中的条目c:\\temp\\.venv

On Linux:在 Linux 上:

>>> import sys
>>> sys.path
['', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/carsten/.venv/lib/python3.8/site-packages']
>>> sys.prefix
'/carsten/.venv'

Notice that the root of the environment is NOT part of the path.请注意,环境的根不是路径的一部分。

Is this difference by design or is this a bug?这是设计上的差异还是这是一个错误?

The Windows case also has the installed prefix directory "C:\Python36", which is added by the interpreter startup code, in addition to the venv prefix directory that's added by site.py.除了 site.py 添加的 venv 前缀目录外,Windows 案例还安装了解释器启动代码添加的前缀目录“C:\Python36”。 Adding these directories is a vestige from long ago.添加这些目录是很久以前的遗迹。 There's no compelling reason that either "C:\Python36" or "C:\temp\.venv" needs to be in sys.path .没有令人信服的理由表明 "C:\Python36" 或 "C:\temp\.venv" 需要在sys.path中。 Though someone somewhere is probably depending on it.尽管某个地方的某个人可能依赖它。

Adding the prefix directory used to be productive in early versions of the Windows release back in the 1990s, which initially put standard-library extension modules in the prefix directory instead of a dedicated "DLLs" subdirectory, plus it also used the prefix directory for site packages.在 1990 年代的 Windows 版本的早期版本中添加前缀目录曾经是有效的,最初将标准库扩展模块放在前缀目录而不是专用的“DLLs”子目录中,此外它还使用前缀目录作为站点包。 For example, see site.py from 1997-08-09.例如,参见 1997-08-09 的site.py。 A few iterations later , "site-packages" was added in Unix, but Windows was still using the prefix directory.几次迭代之后,在 Unix 中添加了“site-packages”,但 Windows 仍然使用前缀目录。

Finally, a few years later on 2001-07-12 , "site-packages" was added to the Windows release of Python 2.2, but in addition to the prefix directory instead of replacing it in sys.path .最后,几年后的2001-07-12 ,“site-packages”被添加到 Python 2.2 的 Windows 版本中,但除了前缀目录,而不是在sys.path中替换它。 This was proposed by Paul Moore (still an active core developer) in PEP 250 , who notes that the prefix directory is being retained in order to support.pth files added there.这是由 Paul Moore(仍然是活跃的核心开发人员)在PEP 250中提出的,他指出保留前缀目录是为了支持在其中添加的 support.pth 文件。

In the current implementation of site.py, addsitepackages calls getsitepackages , for which if the path separator is backslash (a weird way to identify Windows given the better ways available), it first adds prefix and then os.path.join(prefix, libdir, "site-packages") .在当前的 site.py 实现中, addsitepackages调用getsitepackages ,如果路径分隔符是反斜杠(鉴于可用的更好方法,这是一种识别 Windows 的奇怪方法),它首先添加prefix ,然后添加os.path.join(prefix, libdir, "site-packages")

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

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