windows 需要更改 virtualenv 的哪些部分才能重新定位它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6820109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:35:09  来源:igfitidea点击:

What parts of a virtualenv need to be changed to relocate it?

pythonwindowsoopvirtualenvdistutils

提问by jmite

So, I've got a Python program with a ridiculous number of addons/packages. I'd like to be able to distribute the program in its virtualenv, so that the packages come bundled. However, the program is for Windows, and the "relocatable" feature of virtualenvs is unsupported on Windows (as well as still being experimental).

所以,我有一个 Python 程序,里面有大量的插件/包。我希望能够在其 virtualenv 中分发该程序,以便捆绑包。但是,该程序适用于 Windows,并且 Windows 不支持 virtualenvs 的“可重定位”功能(并且仍处于试验阶段)。

So, I'm looking at either writing a script, or just writing instructions to manually change absolute path names to relocate the virtualenv.

因此,我正在考虑编写脚本,或者只是编写说明以手动更改绝对路径名以重新定位 virtualenv。

My question is if anybody knows where all I'd have to look for absolute path names in the virtualenv. I'm pretty new to Python packaging. The activate.bat script contains absolute path names, but do individual packages have absolute pathnames hard-coded into their installations?

我的问题是,是否有人知道我必须在 virtualenv 中查找绝对路径名。我对 Python 打包很陌生。activate.bat 脚本包含绝对路径名,但是单个软件包是否将绝对路径名硬编码到其安装中?

The section Making Environments Relocatabledescribes why a virtualenv can't be simply moved, but doesn't list the places that contain absolute path names.

使环境可重定位一节描述了为什么不能简单地移动 virtualenv,但没有列出包含绝对路径名的位置。

回答by Daniel Roseman

Rather than trying to do this manually, you'd be better off using the freezeoption to pipto create a requirements file. You can now rebuild your entire virtualenv with a single command.

与其尝试手动执行此操作,不如使用创建需求文件的freeze选项pip。您现在可以使用单个命令重建整个 virtualenv。

On old virtualenv:

在旧的 virtualenv 上:

pip freeze > stable-req.txt

On the new one:

在新的:

pip install -r stable-req.txt

回答by Kalyan02

For your virtual environment directory {ENV}follow these steps

对于您的虚拟环境目录,请{ENV}按照以下步骤操作

  1. Run $ virtualenv --relocatable {ENV}
  2. Edit {ENV}/bin/activate, using vim or editor of your choice
  3. Edit VIRTUAL_ENV(around line ~42!)to match the new directory location
  1. $ virtualenv --relocatable {ENV}
  2. 编辑{ENV}/bin/activate,使用vim或者你选择的编辑器
  3. 编辑VIRTUAL_ENV(大约第 42 行!)以匹配新目录位置

So if you are looking to write a script, you can either export VIRTUAL_ENVshell variable or perform the edit on /bin/activate dynamically.

因此,如果您要编写脚本,您可以导出VIRTUAL_ENVshell 变量或动态地在 /bin/activate 上执行编辑。

This is how I've setup mine.

这就是我设置我的方式。

# env/bin/activate
BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# virtual env is at ./env
ENV_DIR=`dirname $BIN_DIR`
VIRTUAL_ENV=$ENV_DIR
export VIRTUAL_ENV

回答by rcoup

virtualenv-toolsis a script to update an existing virtualenv's location after you move it. While it doesn't help on Windows, it might be useful to others.

virtualenv-tools是一个脚本,用于在您移动现有 virtualenv 后更新它的位置。虽然它在 Windows 上没有帮助,但它可能对其他人有用。

It updates:

它更新:

  • the virtualenv activation scripts
  • shebang lines in scripts in the virtualenv bin/directory
  • absolute paths stored in .pyc files
  • symlinks in any local/directory
  • virtualenv 激活脚本
  • virtualenvbin/目录中脚本中的 shebang 行
  • 存储在 .pyc 文件中的绝对路径
  • 任何local/目录中的符号链接