how can I find out which python virtual environment I am using?
Asked Answered
P

5

65

I have several virtual environment in my computer and sometimes I am in doubt about which python virtual environment I am using. Is there an easy way to find out which virtual environment I am connected to?

Pursuivant answered 28/12, 2018 at 0:4 Comment(5)
It should be displayed in the terminal like: (env1)$Nonprofessional
I have many venvs and some I gave the same name and it is confusing me. maybe print sys.executable would tell me?Pursuivant
@kay print sys.prefixTemperance
that does it. thanks!Pursuivant
@Pursuivant great, I've added it as an answerTemperance
T
62

You can use sys.prefix to determine which virtualenv you're in.

import sys
print(sys.prefix)

from the sys docs

A string giving the site-specific directory prefix where the platform independent Python files are installed

Temperance answered 28/12, 2018 at 0:10 Comment(2)
If you need a code one-liner for the command line: python -c "import sys; print(sys.prefix)"Varden
This won't work on Windows with python installed via the scoop package manager, it will show you the path to the user python binary install, instead of the virtual env one. @ShadowRanger's answer will work however.Bascomb
N
22

From a shell prompt, you can just do echo $VIRTUAL_ENV (or in Windows cmd.exe, echo %VIRTUAL_ENV%).

From within Python, sys.prefix provides the root of your Python installation (the virtual environment if active), and sys.executable tells you which Python executable is running your script.

Norsworthy answered 28/12, 2018 at 0:30 Comment(2)
This was the best answer for my problem.Placatory
Adding that on Powershell, this would be echo $env:VIRTUAL_ENV.Bascomb
L
18

Usually it's set to display in your prompt. You can also try typing in which python or which pip in your terminal to see if it points to you venv location, and which one. (Use where instead of which on Windows.)

Languishing answered 28/12, 2018 at 0:6 Comment(2)
On Windows this gave me a list of all available Python environments. The one at the top of the list is the current one. Might seem obvious, but wasn't to me initially.Varden
@Languishing yes but many people use env or venv as the environment name for simplicity, generic aliases, and automation. Which is most likely what warranted this question in the first hand. And in this case, the environment name in the prompt won't help you.Bascomb
J
2

Try

echo $Env:VIRTUAL_ENV

if you are in Windows Powershell (which is for example the default terminal in VSCode).

Taken from Super User at How can I display the contents of an environment variable from the command prompt in Windows 7?

Jack answered 20/5, 2023 at 21:32 Comment(0)
V
0

if you used pip install virtualenvwrapper-win in windows and used: mkvirtualenv <environment_name> , you can just type 'workon' to display the virtual environments on the command line.

Ventriloquy answered 28/12, 2023 at 15:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.