创建没有符号链接的 virtualenv

Creating virtualenv without symlinks

我想创建一个不使用本地系统符号链接的 virtualenv,因为我希望将 virtualenv(包括第三方包)与我的应用程序捆绑在一起。这可能吗?如果可能,怎么做?

例如,在我的 Mac OS X 10.10.2 上,我创建的任何 virtualenv 都包含符号链接:

.Python -> /System/Library/Frameworks/Python.framework/Versions/2.7/Python

如果我直接在与 --always-copy 共享的网络服务器上创建我的 virtualenv,我会得到一个错误:

$ virtualenv --always-copy python2.7.9_win7-64_stalker0.2.13
New python executable in python2.7.9_win7-64_stalker0.2.13/bin/python
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 11, in <module>
    sys.exit(main())
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 825, in main
    symlink=options.symlink)
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 985, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 1374, in install_python
    symlink)
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 482, in copyfile
    copyfileordir(src, dest, symlink)
  File "/Library/Python/2.7/site-packages/virtualenv.py", line 456, in copyfileordir
    shutil.copy2(src, dest)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
    copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
    os.chflags(dst, st.st_flags)
OSError: [Errno 22] Invalid argument: 'python2.7.9_win7-64_stalker0.2.13/.Python'

也许尝试解决更大的问题(合理打包)而不是调整 virtualenv 内部结构?

例如看看http://platter.pocoo.org/ or mayhaps at http://cx-freeze.sourceforge.net/(我不确定你指的是哪种情况)。

使用 Python 3 和 venv 模块,可以使用 --copies flag:

创建一个没有符号链接的 "thick" 虚拟环境
$ python -m venv --copies thick_venv
$ ls -l thick_venv/bin/
total 36836
-rw-r--r--. 1 br0ke br0ke     2230 May 19 17:54 activate
-rw-r--r--. 1 br0ke br0ke     1282 May 19 17:54 activate.csh
-rw-r--r--. 1 br0ke br0ke     2434 May 19 17:54 activate.fish
-rw-r--r--. 1 br0ke br0ke     8832 May 19 17:54 Activate.ps1
-rwxr-xr-x. 1 br0ke br0ke      266 May 19 17:54 easy_install*
-rwxr-xr-x. 1 br0ke br0ke      266 May 19 17:54 easy_install-3.8*
-rwxr-xr-x. 1 br0ke br0ke      248 May 19 17:54 pip*
-rwxr-xr-x. 1 br0ke br0ke      248 May 19 17:54 pip3*
-rwxr-xr-x. 1 br0ke br0ke      248 May 19 17:54 pip3.8*
-rwxr-xr-x. 1 br0ke br0ke 18833904 May 19 17:54 python*
-rwxr-xr-x. 1 br0ke br0ke 18833904 May 19 17:54 python3*

如您所见,它将解释器复制到虚拟环境中(两次)。这是带有符号链接的常用虚拟环境的示例:

$ python -m venv thin_venv
$ ls -l thin_venv/bin/
total 44
-rw-r--r--. 1 br0ke br0ke 2227 May 19 17:54 activate
-rw-r--r--. 1 br0ke br0ke 1279 May 19 17:54 activate.csh
-rw-r--r--. 1 br0ke br0ke 2431 May 19 17:54 activate.fish
-rw-r--r--. 1 br0ke br0ke 8832 May 19 17:54 Activate.ps1
-rwxr-xr-x. 1 br0ke br0ke  265 May 19 17:54 easy_install*
-rwxr-xr-x. 1 br0ke br0ke  265 May 19 17:54 easy_install-3.8*
-rwxr-xr-x. 1 br0ke br0ke  247 May 19 17:54 pip*
-rwxr-xr-x. 1 br0ke br0ke  247 May 19 17:54 pip3*
-rwxr-xr-x. 1 br0ke br0ke  247 May 19 17:54 pip3.8*
lrwxrwxrwx. 1 br0ke br0ke   50 May 19 17:54 python -> /home/br0ke/.asdf/installs/python/3.8.3/bin/python*
lrwxrwxrwx. 1 br0ke br0ke    6 May 19 17:54 python3 -> python*