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

[doc] Document VIRTUAL_ENV environment variable (GH-21970) #21970

Merged
merged 1 commit into from
Sep 1, 2020

Conversation

andresdelfino
Copy link
Contributor

@andresdelfino andresdelfino commented Aug 26, 2020

Automerge-Triggered-By: @vsajip

@vsajip vsajip changed the title [doc] Document VIRTUAL_ENV environment variable [doc] Document VIRTUAL_ENV environment variable (GH-21970) Sep 1, 2020
@vsajip vsajip merged commit 3584d4b into python:master Sep 1, 2020
@andresdelfino andresdelfino deleted the document_virtual_env_envvar branch September 1, 2020 13:01
xzy3 pushed a commit to xzy3/cpython that referenced this pull request Oct 18, 2020
@miss-islington
Copy link
Contributor

Thanks @andresdelfino for the PR, and @vsajip for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

Thanks @andresdelfino for the PR, and @vsajip for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-24362 is a backport of this pull request to the 3.9 branch.

@bedevere-bot
Copy link

GH-24363 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 28, 2021
(cherry picked from commit 3584d4b)

Co-authored-by: Andre Delfino <adelfino@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 28, 2021
(cherry picked from commit 3584d4b)

Co-authored-by: Andre Delfino <adelfino@gmail.com>
miss-islington added a commit that referenced this pull request Jan 30, 2021
(cherry picked from commit 3584d4b)

Co-authored-by: Andre Delfino <adelfino@gmail.com>
vsajip pushed a commit that referenced this pull request Mar 1, 2021
@pelson
Copy link
Contributor

pelson commented Oct 6, 2022

@vsajip - sorry to drag this up, but I believe this change is incorrect. It would be dangerous to use an environment variable to determine if a virtual environment is being used or not. I would never do this in production, and instead would check sys.base_prefix != sys.prefix.

This recommendation is indeed already explicitly stated in the same file:

When a virtual environment is active (i.e., the virtual environment’s Python interpreter is running), the attributes sys.prefix and sys.exec_prefix point to the base directory of the virtual environment, whereas sys.base_prefix and sys.base_exec_prefix point to the non-virtual environment Python installation which was used to create the virtual environment.

A simple counter example to the documentation would be:

$ python3.9 -m venv /tmp/a-new-venv
$ /tmp/a-new-venv/bin/python -c "import os; print(f'Val: {os.getenv(\"VIRTUAL_ENV\")}')"
Val: None

I therefore recommend that this commit be reverted. I would be happy to file a bug if that is required to revert.

@vsajip
Copy link
Member

vsajip commented Oct 6, 2022

I therefore recommend that this commit be reverted.

Rather than doing that, I would propose to change the paragraph as follows:

When a virtual environment has been activated using one of the activation scripts, the :envvar:VIRTUAL_ENV environment
variable is set to the path of the virtual environment. Although it might seem that this could be used to check if one is running inside a virtual environment, it is not always present: for example, a script installed in a virtual environment can be run using its absolute path, and the correct environment would then be in effect; however, there would be no VIRTUAL_ENV variable in the environment. It's always safer to use sys.base_prefix != sys.prefix, which will be True when running in a virtual environment. See the note below.

@pelson
Copy link
Contributor

pelson commented Oct 6, 2022

When a virtual environment has been activated using one of the activation scripts, the :envvar:VIRTUAL_ENV environment
variable is set to the path of the virtual environment. Although it might seem that this could be used to check if one is running inside a virtual environment, it is not always present: for example, a script installed in a virtual environment can be run using its absolute path, and the correct environment would then be in effect; however, there would be no VIRTUAL_ENV variable in the environment. It's always safer to use sys.base_prefix != sys.prefix, which will be True when running in a virtual environment. See the note below.

I take your lead on this, but to me that sounds like quite a lot of words for not a lot of new detail. I definitely think the page in question could be reshuffled a bit to make this concise and easier to find the information quickly. Would you like me to submit a proposal?

@vsajip
Copy link
Member

vsajip commented Oct 6, 2022

Would you like me to submit a proposal?

By all means.

@andresdelfino
Copy link
Contributor Author

I think we shouldn't remove the mention to VIRTUAL_ENV (reverting this commit would have that effect), as it is helpful.

Of course, I'm all for improving the correctness of the docs.

@vsajip
Copy link
Member

vsajip commented Oct 6, 2022

Well, I made my suggestion in the way that I did because it

  • Informs users about the environment variable (it could be useful to know in some scenarios)
  • Warns against relying on it for determining whether code is running in a virtual environment

and it may be wordier than it needs to be, but I was thinking of spelling things out for any inexperienced users / users new to virtual environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants