Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PIPENV_VENV_IN_PROJECT on per-project basis

I want pipenv to create its virtualenv in $PROJECTDIR/.venv automatically for everyone who checks out the project.

So far, I see only the following options working, none of which is satisfying:

  • Ask users to set PIPENV_VENV_IN_PROJECT=1 globally, forcing my project preferences on each of their other projects.
  • Ask users to always invoke pipenv via "PIPENV_VENV_IN_PROJECT=1 pipenv" when inside my project, which begs for trouble if they run a pipenv command and forget to set PIPENV_VENV_IN_PROJECT.
  • Ask users to install a third-party tool direnv and provide an .envrc file, adding yet another non-obvious build dependency.

I tried the following approaches:

  • Add an PIPENV_VENV_IN_PROJECT-like option to Pipfile, but I couldn't find any such Pipfile option.
  • Put "PIPENV_VENV_IN_PROJECT=1" into $PROJECTDIR/.env, but that is only evaluated inside the sub shell of pipenv shell/run, and is ignored by pipenv itself.

So, how do I set PIPENV_VENV_IN_PROJECT on per-project basis?

like image 344
vog Avatar asked Sep 13 '19 07:09

vog


Video Answer


1 Answers

To answer this question myself, this is indeed possible!

Short story:

Just create an empty .venv directory yourself. Pipenv will pick this up automaticaly.

Long story:

Note that there are many issues in the pipenv issue tracker that give the wrong impression that the pipenv developers don't want to provide this feature. However, those issues simply describe additional ways to achieve this goal, and those additional ways were declined.

But within that mess, there was one comment that was actually helpful:

  • https://github.com/pypa/pipenv/issues/2197#issuecomment-446601760

techalchemy commented on Dec 12, 2018

Things you can do:

  1. Simply create the .venv yourself. Pipenv will use it.
  2. Use a tool like direnv to activate this setting per directory
  3. Set the envvar globally
  4. Create a file in the project root called .venv whose contents are only the path to the root directory of a virtualenv

For points 1 and 4, pipenv will pick this up automatically

Note: If you want to use the pipenv shipped with current Debian/Stable (Buster), point 4 won't work, as this feature was introduced in a later pipenv version. However, point 1 works perfectly well. For Python 3, this means:

python3 -m virtualenv -p python3 .venv
pipenv install ...  # resp. pipenv sync
like image 190
vog Avatar answered Nov 04 '22 10:11

vog