ยท 1 min read
Rename a Python virtualenv
virtualenv does not support renaming. Recreating it is faster and safer than editing paths by hand.
mv venv venv2 looks harmless and breaks things later: the absolute path is baked into bin/activate and into the shebang of every console script inside bin/. The environment still works for python -m calls, then fails the moment you run pip or a CLI installed in it.
Recreate it
source vnev/bin/activate
pip freeze > requirements.txt
deactivate
rm -r vnev/
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip freeze writes exactly what is installed, including versions. Keep requirements.txt outside the directory you delete โ if you write it inside vnev/, step 4 removes it.
If you cannot recreate it
Two third-party tools handle the path rewriting:
virtualenv-mv vnev venv
or, with virtualenvwrapper:
cpvirtualenv vnev venv
Both copy the environment and rewrite the paths in place. Recreating from requirements.txt is still the cleaner option: the new environment is compiled against the current Python, and you get a file that documents what the project needs.
Check your shell
pip freeze while the wrong environment is active records the wrong packages. which python before step 1 is worth the second it costs.