Search code examples
pipenv

Manage the path to the venv for pipenv


Is it possible to tell pipenv where the venv is located? Perhaps there's something you can put in the pipfile, or something for the .env file?

I fairly frequently have to recreate my venv because pipenv seemingly loses track of where it is.

For example, I started a project using Pycharm to configure the file system and create my pipenv interpreter. It created the venv in ~/.local/share/virtualenvs/my-project-ZbEWMGNA and it was able to keep track of where that interpreter was located.

Switching to a terminal window & running pipenv commands then resulted in;

Warning: No virtualenv has been created for this project yet! Consider running pipenv install first to automatically generate one for you or seepipenv install --help for further instructions.

At which point I ran pipenv install from the terminal & pointed pycharm at that venv, so the path would become ~/apps/my-project-ZbEWMGNA (which sits alongside the project files ~/apps/my-project)

Now I've got venvs in both paths and pipenv still can't find them.

mwalker@Mac my-project % pipenv --where
/Users/mwalker/apps/my-project

mwalker@Mac my-project % pipenv --venv 
No virtualenv has been created for this project yet!
Aborted!

mwalker@Mac my-project % ls ~/apps
my-project
my-project-ZbEWMGNA

mwalker@Mac my-project % ls ~/.local/share/virtualenvs 
my-project-ZbEWMGNA

Solution

  • Yes, it is possible by setting environment variables. You can set a path for virtual environments via the WORKON_HOME. Or have the virtual environment created in the project with PIPENV_VENV_IN_PROJECT.

    Pipenv automatically honors the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want

    -- https://pipenv-fork.readthedocs.io/en/latest/advanced.html#custom-virtual-environment-location

    or

    PIPENV_VENV_IN_PROJECT If set, creates .venv in your project directory.

    -- https://pipenv-fork.readthedocs.io/en/latest/advanced.html#pipenv.environments.PIPENV_VENV_IN_PROJECT

    In my experience, PyCharm will uses the existing venv created by Pipenv. Otherwise it will create it in the directory that PyCharm is configured to create it.