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

耗尽温柔 提交于 2019-11-28 08:26:32

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

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

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