问题
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 apipenv
command and forget to setPIPENV_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 ofpipenv shell/run
, and is ignored bypipenv
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:
- Simply create the .venv yourself. Pipenv will use it.
- Use a tool like direnv to activate this setting per directory
- Set the envvar globally
- 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