Persisting module installation across new python virtual environments

387 views Asked by At

For background, I work mostly in the terminal and edit code using vim. For long-term python projects I manage venvs and lock files in source control using poetry.

There are some modules I like to have installed in almost every venv I work in, such as ipython/ptpython, (pytest-)icdiff, and other "quality of life" extensions that I need not foist on project collaborators who don't use my workflow. I can install ptpython in the global scope using my distro's package manager (or pipx), but then when I run it, it does not run inside the local venv and local dependencies are not accessible. This gets obnoxious since I'm periodically recreating venvs as the lock files change. Right now I have a shell script that installs the things, but that feels like a hack.

An ideal solution might be a way to create something like a venv template, similar to the git templatedir option. Is there anything of such for Python?

3

There are 3 answers

0
causaSui On

The virtualenv module has an option to include system site packages in the venv:

python3 -m venv --system-site-packages .venv

There is a feature request to have support for this option added to poetry.

There is a nice overview of site packages in this thread.

0
shiv On

This doesn't directly answer maybe but I think anaconda environments are the way to go. You could create different 'base' environments, then clone them. Then you could add more packages using pip as you'd like on the new cloned environment, thereby not changing the base, but having customizability over new enviroment.

Try this answer: https://stackoverflow.com/a/40702703/7654532

0
pygeek On

Solution

This problem isn’t specific to Python venv, it’s present in rvm and nvm also. Just install the package under the global Python namespace and add it to your PYTHONPATH so that if the package isn’t installed in the local repository Python falls back to your global Python namespace without modifying the repository lockfile.