virtualenv 在 Ubuntu 上创建名为 'local' 的目录

virtualenv creates directory called 'local' on Ubuntu

如果我使用 Ubuntu 18.04 创建 virtualenv,则会创建一个名为 local 的目录。

virtualenv test-env
cd test-env
ls -l local/

输出:

lrwxrwxrwx 1 foo foo 30 Jan 30 10:47 bin -> /home/foo/tmp/test-env/bin
lrwxrwxrwx 1 foo foo 34 Jan 30 10:47 include -> /home/foo/tmp/test-env/include
lrwxrwxrwx 1 foo foo 30 Jan 30 10:47 lib -> /home/foo/tmp/test-env/lib

版本:

virtualenv --version
15.0.3

这不会发生在其他机器上(例如企业版 SuSE Linux)

据我所知不需要此目录。

有没有办法避免这个不需要的名为 local 的目录?

(这大约是 Python 2.7)

在Ubuntu中,virtualenv 模仿机器的安装,local 是其中的一部分。 如果你想在你的项目中忽略这个,你可以将它添加到 .gitignore

通过文档和一些较旧的 SO 帖子进行挖掘,我偶然发现了 this answer, and the official Release Notes

在我的 Ubuntu 16.04virtualenv 15.0.1 组合中,~/.local 文件夹包含所有 Python 相关的库、文档和二进制文件。

我怀疑 virtualenv 正在尝试 "match" 这种方法,以便在裸系统上保持与脚本 运行 的兼容性,因为 Python $PATH 看起来Python 相关模块、包等

Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)

/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
...
/home/<username>/.local/lib/python2.7/site-packages
...
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages

如果我 运行 在我的 virtualenv 中使用相同的命令,我会在我的 Python 路径中得到两个目录。

Python 2.7.12 (default, Dec  4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print '\n'.join(sys.path)

/home/<username>/python-venv-tests/lib/python2.7
/usr/lib/python2.7
...
/home/<username>/python-venv-tests/local/lib/python2.7/site-packages
/home/<username>/python-venv-tests/lib/python2.7/site-packages
...

所以,回答你的问题

  • .local 目录仅包含指向虚拟环境 'proper' /bin, /include, /lib 的符号链接,因此不存在重复库的危险
  • 这很可能是作为 Ubuntu 对 Python 包
  • 使用 ~/.local 的兼容性措施
  • 这意味着如果确实需要,您可以随意删除此 and/or 更改您的 Python $PATH,但这不会对您的工作流程产生任何影响(或者如果您计划将虚拟环境迁移到其他主机)。
  • 一般来说,我建议保持原样,以便虚拟环境尽可能匹配系统结构,而其他 scripts/users 可能具有取决于它的硬编码值避免错误。