Problem:
My program is getting a list of (requirements_123.txt
, program_123.py
) pairs (actually a list of script lines like pip install a==1 b==2 c==3 && program_123.py
).
My program needs to run each program in an isolated virtual environment based on the current environment.
Requirements:
- Current environment is not modified
- Program environment is based on the current environment
- Not reinstalling the packages from the current env. It's slow. It does not really work (package sources might be missing, build tools might be missing). No
pip freeze | pip install
please - Fast. Copying gigabytes of files from current environment to a new environment every time is too slow. Symlinking might be OK as a last resort.
Ideal solution: I set some environment variables for each program, pointing to a new virtual environment dir, and then just execute the script and pip does the right thing.
How can I do this?
What do I mean by "overlay": Python already has some "overlays". There are system packages and user packages. User packages "shadow" the system packages, but non-shadowed system packages are still visible to the programs. When pip installs the packages in the user directory, it does not uninstall the system package version. This is the exact behavior I need. I just need a third overlay layer: "system packages", "user packages", "program packages".
Related questions (but they do not consider the user dir packages, only the virtual environments):
"Cascading" virtual Python environnements Is it possible to create nested virtual environments for python?
P.S.
If pip freeze doesn't even work, you have much larger problems lurking.
There are many reasons why the result of pip freeze > requirements.txt
does not work in practice:
- System-installed packages installed using
apt
. - Packages installed from different package indexes, not PyPI (PyTorch does that). Package
conda-package-handling
is not on PyPI. - Conda packages.
- Packages built from source some time ago (and your compilers are different now).
- Installs from git or zip/whl files.
- Editable installs.
I've just checked a default notebook instance in Google Cloud and almost half of the pip freeze
list looks like this:
threadpoolctl @ file:///tmp/tmp79xdzxkt/threadpoolctl-2.1.0-py3-none-any.whl
tifffile @ file:///home/conda/feedstock_root/build_artifacts/tifffile_1597357726309/work
Also packages like conda-package-handling
are not even on PyPI.
Anyways, this is just one of the many reasons why pip freeze | pip install
does not work in practice.