Create virtualenvs in project directories


It's quite handy to create the virtual environment aka venv for a python project in the project folder itself. This means:

  1. No huge .virtualenvs folder in your home directory with a lot of envs which you will never use again (The one on this computer is 1.2 GB big although I do not work with many Python projects at all(!)). If you are done with a project and delete all local files, the corresponding environment will be gone with it.

  2. Pointing your editor/IDE to the current virtual env as well as deleting it is easy, because it is right there in the project root folder.

As there are many tools in this realm, obviously every one of them handles things a little bit differently and it is the default for none of the popular ones:

Poetry

Poetry makes it easy for you to express you preference:

poetry config virtualenvs.in-project true

This saves your preference directly to ~/.config/pypoetry/config.toml, which then looks like this:

[virtualenvs]
in-project = true

Pipenv

For Pipenv one has to set the environment variable PIPENV_VENV_IN_PROJECT. This one is also pretty popular to create Docker images from Pipenv projects.

Conda

Conda makes it harder by not having a global switch for this option. Apparently one has to use the --prefix or -p flags instead of --name or -n.


See also