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?
-
It should be displayed in the terminal like: `(env1)$` – Alan Kavanagh Dec 28 '18 at 00:05
-
I have many venvs and some I gave the same name and it is confusing me. maybe print sys.executable would tell me? – Kay Dec 28 '18 at 00:07
-
1@kay `print sys.prefix` – wpercy Dec 28 '18 at 00:08
-
that does it. thanks! – Kay Dec 28 '18 at 00:09
-
@Kay great, I've added it as an answer – wpercy Dec 28 '18 at 00:10
4 Answers
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

- 7,332
- 3
- 48
- 69

- 9,636
- 4
- 33
- 45
-
6If you need a code one-liner for the command line: `python -c "import sys; print(sys.prefix)"` – jshd Nov 23 '22 at 14:15
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.)

- 10,813
- 1
- 23
- 28
-
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. – jshd Nov 23 '22 at 14:16
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.

- 143,180
- 12
- 188
- 271
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?

- 7,175
- 4
- 57
- 90