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
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:
Why VIRTUAL_ENV is not enough
You will often see this:
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:
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
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.
Related reading
- How to determine whether a Pandas Column contains a particular value
- How to disable keras warnings?
- How to disable Python warnings?
- How to display a float with two decimal places?
- How to display pandas DataFrame of floats using a format string for columns?
- How to display the current year in a Django template?
- How to display training progress bar in tensorflow?
- How to divide flask app into multiple py files?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.