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

不问归期 提交于 2019-11-27 02:12:43

问题


This question already has an answer here:

  • Renaming a virtualenv folder without breaking it 8 answers

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).

So, I'm looking at either writing a script, or just writing instructions to manually change absolute path names to relocate the 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?

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


回答1:


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

On old virtualenv:

pip freeze > stable-req.txt

On the new one:

pip install -r stable-req.txt



回答2:


For your virtual environment directory {ENV} follow these steps

  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

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

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



回答3:


virtualenv-tools is 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.

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


来源:https://stackoverflow.com/questions/6820109/what-parts-of-a-virtualenv-need-to-be-changed-to-relocate-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!