Can I move a virtualenv?

蓝咒 提交于 2019-11-28 03:54:10

Yes. It is possible to move it on the same platform. You can use --relocatable on an existing environment.

From --help:

--relocatable -- Make an EXISTING virtualenv environment relocatable. This fixes up scripts and makes all .pth files relative.

HOWEVER, this does NOT seem to change the activate script, and rather only changes the pip* and easy_install* scripts. In the activate script, the $VIRTUAL_ENV environment variable hardcoded as the original /path/to/original/venv. The $VIRTUAL_ENV variable is used to set the PATH of your active environment too, so it must be changed based on the new location in order to call python and pip etc. without absolute path.

To fix this issue, you can change the $VIRTUAL_ENV environment variable in the activate script (for example using sed), and everything should be good to go.

An example of usage:

$ cd ~/first
$ virtualenv my-venv
$ grep 'VIRTUAL_ENV=' my-venv/bin/activate
VIRTUAL_ENV="/home/username/first/my-venv"
$ virtualenv --relocatable my-venv
Making script my-venv/bin/easy_install relative
Making script my-venv/bin/easy_install-2.7 relative
Making script my-venv/bin/pip relative
Making script my-venv/bin/pip2 relative
Making script my-venv/bin/pip2.7 relative
### Note that `activate` has not been touched
$ mkdir ~/second
$ mv my-venv ~/second
$ cd ~/second
$ grep 'VIRTUAL_ENV=' my-venv/bin/activate
VIRTUAL_ENV=/home/username/first/my-venv
### (This variable hasn't been changed, it still refers to the old, now non-existent directory!)
$ sed -i -e 's|username/first|username/second|' my-venv/bin/activate
## sed can be used to change the path.
## Note that the `-i` (in place) flag won't work on all machines. 
$ source my-venv/bin/activate 
(my-venv) $ pip install foass
...
(my-venv) $ python 
[...]
> import foass

Hooray, now you can install things and load them into your newly located virtual environment.

The --relocatable argument to virtualenv appears to allow you to do this.

Yes, this should be possible if you haven't done anything that depends on the current directory of the virtualenv.

However, if you have the choice, the best thing to do is to create new virtualenv and start using the new virtualenv instead. This is the safest choice and least likely to cause issues later.

The documentation does mention that:

Each virtualenv has path information hard-coded into it,

For example, if you have run setvirtualenvproject then it won't be able to switch to the right directory after you run workon ... so in that case you'd need to fix that manually.

In general a virtualenv is little more than a directory with the necessary Python interpreter files plus packages that you need.

Nathan Basanese

BUT ALAS:

No you can't simply mv. There are workarounds, but it might be easier to reinstall.

(my-python-venv)$ /home/me/PeskyPartyPEnvs/pip3 install foaas
zsh: /home/me/PeskyPartyPEnvs/pip3: bad interpreter: /home/me/Env/my-python-venv/bin/python3: no such file or directory
(my-python-venv)$ deactivate
$ 

... presses enter a lot in frustration, and the following works

$
$
$ pip3 search foaas

Except it is not from my-python-venv, ergo sadness.

Want to mv your virtualenv and use it, otherwise unmodified?

Short Answer:

Well, ya can't.

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