7

也许我不清楚 virtualenv 是如何工作的,但是如果我使用 --always-copy 标志运行 virtualenv,我希望 /lib/python 中的所有文件都被复制。

所以我创建了一个虚拟环境

virtualenv -v --always-copy myenv

的输出是

Creating myenv/lib/python2.7
Symlinking Python bootstrap modules
  Copying to myenv/lib/python2.7/config
  Copying to myenv/lib/python2.7/lib-dynload
  Copying to myenv/lib/python2.7/os.py
  Ignoring built-in bootstrap module: posix
  Copying to myenv/lib/python2.7/posixpath.py
  Cannot import bootstrap module: nt
  Copying to myenv/lib/python2.7/ntpath.py
  Copying to myenv/lib/python2.7/genericpath.py
  Copying to myenv/lib/python2.7/fnmatch.py
  Copying to myenv/lib/python2.7/locale.py
  Copying to myenv/lib/python2.7/encodings
  Copying to myenv/lib/python2.7/codecs.py
  Copying to myenv/lib/python2.7/stat.py
  Copying to myenv/lib/python2.7/UserDict.py
  File myenv/lib/python2.7/lib-dynload/readline.so already exists
  Copying to myenv/lib/python2.7/copy_reg.py
  Copying to myenv/lib/python2.7/types.py
  Copying to myenv/lib/python2.7/re.py
  Copying to myenv/lib/python2.7/sre.py
  Copying to myenv/lib/python2.7/sre_parse.py
  Copying to myenv/lib/python2.7/sre_constants.py
  Copying to myenv/lib/python2.7/sre_compile.py
  File myenv/lib/python2.7/lib-dynload/zlib.so already exists
  Copying to myenv/lib/python2.7/warnings.py
  Copying to myenv/lib/python2.7/linecache.py
  Copying to myenv/lib/python2.7/_abcoll.py
  Copying to myenv/lib/python2.7/abc.py
  Copying to myenv/lib/python2.7/_weakrefset.py
Creating myenv/lib/python2.7/site-packages
Writing myenv/lib/python2.7/site.py
Writing myenv/lib/python2.7/orig-prefix.txt
Writing myenv/lib/python2.7/no-global-site-packages.txt
Creating parent directories for myenv/include
Copying to myenv/include/python2.7
Creating myenv/bin
New python executable in myenv/bin/python
Changed mode of myenv/bin/python to 0755
Copying to myenv/bin/python2
Copying to myenv/bin/python2.7
Testing executable with myenv/bin/python -c "import sys;out=sys.stdout;getattr(out, "buffer", out).write(sys.prefix.encode("utf-8"))"
Got sys.prefix result: u'/home/rbouza/myenv'
Creating myenv/lib/python2.7/distutils
Writing myenv/lib/python2.7/distutils/__init__.py
Writing myenv/lib/python2.7/distutils/distutils.cfg
Installing setuptools, pip...
  Running command /home/rbouza/myenv/bin/python -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip
  Ignoring indexes: https://pypi.python.org/simple/
  Downloading/unpacking setuptools
  Downloading/unpacking pip
  Installing collected packages: setuptools, pip
  Successfully installed setuptools pip
  Cleaning up...
...Installing setuptools, pip...done.
Writing myenv/bin/activate
Writing myenv/bin/activate.fish
Writing myenv/bin/activate_this.py
Writing myenv/bin/activate.csh

然后我去lib目录

cd myenv/lib/python2.7

列出所有文件

_abcoll.py   config        fnmatch.pyc      locale.py                    os.pyc     
    site.py            sre_parse.py   types.pyc       _weakrefset.pyc
_abcoll.pyc  copy_reg.py   genericpath.py   locale.pyc                   posixpath.py   site.pyc           sre_parse.pyc  UserDict.py
abc.py       copy_reg.pyc  genericpath.pyc  no-global-site-packages.txt  posixpath.pyc  sre_compile.py     sre.py         UserDict.pyc
abc.pyc      distutils     lib-dynload      ntpath.py                    re.py          sre_compile.pyc    stat.py        warnings.py
codecs.py    encodings     linecache.py     orig-prefix.txt              re.pyc         sre_constants.py   stat.pyc       warnings.pyc
codecs.pyc   fnmatch.py    linecache.pyc    os.py                        site-packages  sre_constants.pyc  types.py       _weakrefset.py

确切地说:包括目录在内的 50 个,但是当我计算 python 安装目录中的文件/目录时,我得到 200+(仅 1 级,不包括站点包等)

有谁知道为什么这是这种行为?

甚至__future__.py都不存在。

谢谢你。

4

2 回答 2

3

您不会期望 virtualenv 对您的 python 库进行不必要的完全复制。

您的虚拟 Python 仍会从您的根 Python 环境中引用和读取库文件。virtualenv 所做的是将您的新库安装路径设置在虚拟环境中,因此进一步的库安装将被限制在此虚拟环境中。

--always-copy

此选项仅使 virtualenv 复制必要的文件而不是符号链接。

于 2015-11-03T15:07:08.870 回答
0

这是来自手册

--always-copy 总是复制文件而不是符号链接。

关键是通常没有这个选项 virtualenv 应该只符号链接最少需要的文件以节省空间。然后,此选项可确保这些文件的副本。

它没有说明复制其余文件(尤其是您安装的额外站点包)。

您需要使用 --always-copy 看起来除了您的新/额外项目包之外,您可能还想使用 Python 的系统站点包。

有这个选项:

如果您使用 virtualenv --system-site-packages ENV 构建,您的虚拟环境将从 /usr/lib/python2.7/site-packages(或您的全局站点包目录所在的任何位置)继承包。

于 2015-11-03T15:29:42.610 回答