Discover, freeze and rebuild venvs to preserve them when installing a new Python release (e.g. Python 3.7 --> Python 3.8)
This script has not been thoroughly tested! Use it at your own risk!!!
The purpose of this script is to automatically:
- discover venvs/virtualenvs that exist in your system
- backup their configuration, i.e. freeze their requirements in
a
requirements.txt
file located outside the venv itself - rebuild venvs from the backed up
requirements.txt
files
The main use case is dealing with a minor Python upgrade, e.g. from Python 3.7 to Python 3.8 with a rolling release Linux distribution such as Manjaro Linux or Arch.
WARNING: this script will always build venvs, not virtualenvs, even if the backed up thing was a virtualenv. In some cases this is not what you want. For example, pipenv manages virtualenv but is not able to manage venvs. Be sure to remove virtualenvs thay you do not want to rebuild as venvs, as explained below.
Here is the intended usage recipe:
-
Important: edit script configuration. Specify the list of directories where venvs will be looked for, and the directory where your requirements will be backed up. Search is recursive
-
Run:
python upgrade_venv.py freeze
-
Review your destination directory. You will find a copy of your filesystem structure, populated only with venv folders and requirements.txt files. You can delete folders corresponding to venvs/virtualenvs that you do not want to rebuild later
-
Run:
python upgrade_venv.py rebuild
The script will archive the old content of your venvs (such as directoriesbin
,lib
,include
) in a subdirectoryvenv_archive
of each venv. Then it will create a new venv and reinstall requirements usingpip
-
Inspect and test your venvs to make sure that they work as intended
-
If you really wish to remove your old archived venv stuff, run:
python upgrade_venv.py cleanup