Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass VIRTUAL_ENV environment variable to commands #245

Conversation

cjolowicz
Copy link
Collaborator

@cjolowicz cjolowicz commented Sep 1, 2019

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 the bin/activate scripts provided with virtual environments created by virtualenv and the core venv 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 standard venv module.

Implementation

The variable is added to the env attribute of VirtualEnv instances. It will be available to commands created by the session.run and session.install functions, through session.env which references the virtualenv created by the SessionRunner.

Limitations

This change is only applied to virtual environments (VirtualEnv), not to conda environments (CondaEnv), because I am not familiar with them.

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. More specifically, add the
variable to the `env` attribute of `VirtualEnv` instances.

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 usually set in the user's shell by the
bin/activate scripts provided with a virtualenv. 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.
@cjolowicz cjolowicz mentioned this pull request Sep 1, 2019
1 task
@theacodes
Copy link
Collaborator

I thought we already did this, so, cool. Thanks, @cjolowicz!

@theacodes theacodes merged commit d4efd4c into wntrblm:master Sep 3, 2019
@cjolowicz cjolowicz deleted the pass-virtual-env-environment-variable-to-commands branch September 4, 2019 09:23
@cjolowicz
Copy link
Collaborator Author

Thank you for this awesome project!

@cjolowicz
Copy link
Collaborator Author

@theacodes Are you planning a release anytime soon? I would love to see this feature in stable Nox.

@theacodes
Copy link
Collaborator

2019.11.9 is out: #263

Thank you so much. 💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

pipenv support
2 participants