Pass VIRTUAL_ENV environment variable to commands #245
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add the
VIRTUAL_ENV
variable to the process environment of commands executed in a nox session. The value of this variable contains the location of the virtualenv created by nox.Closes #159
Rationale
Some commands rely on the presence of the
VIRTUAL_ENV
variable in their environment to detect that they are running in a virtualenv. In particular, poetry and pipenv will only install packages into the virtualenv if its location is passed to them via this variable. Without that variable, these commands will create their own virtualenv for the project, breaking session isolation.The
VIRTUAL_ENV
variable is set in the user's shell by thebin/activate
scripts provided with virtual environments created byvirtualenv
and the corevenv
module. In the case of nox, commands run in a session are not executed within a shell. So the variable needs to be provided to the command by nox itself.The
VIRTUAL_ENV
variable is not (AFAICT) prescribed by any PEP, but it is mentioned in PEP 486 and used in the implementation of the standardvenv
module.Implementation
The variable is added to the
env
attribute ofVirtualEnv
instances. It will be available to commands created by thesession.run
andsession.install
functions, throughsession.env
which references the virtualenv created by theSessionRunner
.Limitations
This change is only applied to virtual environments (
VirtualEnv
), not to conda environments (CondaEnv
), because I am not familiar with them.