Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

If I am creating a virtualenv, and moving it to a separate folder, will it work?

$ virtualenv -p /usr/bin/python3 /home/me/Env/my-python-venv

$ source Env/my-python-venv/bin/activate

(my-python-venv) $ 

...later that day, the virtual environment MOVED...

(my-python-venv) $ deactivate

$ mkdir -p /home/me/PeskyPartyPEnvs

$ mv /home/me/Env/my-python-venv /home/me/PeskyPartyPEnvs/

Will this work?

$ source /home/me/PeskyPartyPEnvs/my-python-venv/bin/activate

(my-python-venv) $ /home/me/PeskyPartyPEnvs/my-python-venv/bin/pip3 install foaas

1 Answer

0 votes
by (108k points)

Kindly be informed that yes, it is possible to move it on the same platform. You can simply use --relocatable on an existing environment. But it will get changed based on the new location in order to call python and pip, etc. without the absolute path.

To repair this problem, you can adjust 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

Join this Python Training course now if you want to gain more knowledge in Python.

Related questions

0 votes
1 answer
asked Jul 12, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 21, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer

Browse Categories

...