How to set PIPENV_VENV_IN_PROJECT on per-project basis

[亡魂溺海] 提交于 2021-02-19 02:15:36

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/57919110/how-to-set-pipenv-venv-in-project-on-per-project-basis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!