Python
virtualenv
programming
environment
tutorial

How to check if Python is running inside a virtual environment (venv)

Codemia Knowledge Hub ·

Compare sys.prefix with sys.base_prefix: if they differ, Python is running inside a virtual environment. Environment variables such as VIRTUAL_ENV can help, but they are easier to lose or fake. The exact check depends slightly on whether you care about venv, old virtualenv, or broader environment managers such as Conda.

The sys.prefix check for venv

python
1import sys
2
3inside_venv = sys.prefix != sys.base_prefix
4print(inside_venv)

Outside a virtual environment, sys.prefix and sys.base_prefix are normally the same. Inside a venv, sys.prefix points at the environment while sys.base_prefix points at the underlying base interpreter. This is the cleanest general-purpose check for current Python versions.

Supporting older virtualenv

Older virtualenv setups sometimes expose sys.real_prefix instead. Checking only sys.real_prefix misses modern venv, so combine both:

python
1import sys
2
3inside_virtualenv = (
4    hasattr(sys, "real_prefix") or
5    sys.prefix != getattr(sys, "base_prefix", sys.prefix)
6)
7
8print(inside_virtualenv)

Why VIRTUAL_ENV is not enough

You will often see this:

python
import os

print("VIRTUAL_ENV" in os.environ)

It can work, but environment variables depend on how the process was started and whether the activation script was used. A Python process can run inside a virtual environment with VIRTUAL_ENV absent, especially when the interpreter was invoked directly. Checking from the shell prompt tells you about the shell, not about how Python itself was launched, so interpreter-based checks are generally better.

Detecting Conda separately

Conda environments are a different implementation from venv and use different signals. A simple process-level clue is:

python
1import os
2
3inside_conda = "CONDA_PREFIX" in os.environ
4print(inside_conda)

If your real question is "am I inside any managed Python environment," combine the venv-style and Conda-style checks. If it is specifically about virtualenv or venv, stick to the sys-based logic.

A reusable helper

python
1import sys
2
3
4def in_virtualenv() -> bool:
5    return (
6        hasattr(sys, "real_prefix") or
7        sys.prefix != getattr(sys, "base_prefix", sys.prefix)
8    )
9
10print(in_virtualenv())

This suits setup scripts, diagnostics, or tools that should warn when run against the system interpreter. Typical uses:

  • warning users before installing packages globally
  • collecting environment diagnostics
  • changing default paths or cache locations
  • making setup scripts behave differently inside isolated environments

Many applications never need this check. Also treat it as a convenience and configuration check, not a security or trust boundary.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms