Python Practice › sys Module Programs › Program 16
Which Python is running?
Write a Python program that checks which interpreter file is running.
Output
True True -> not in a virtual environment
Approach
sys.executable.
Solution
import osimport sys print(os.path.basename(sys.executable).lower().startswith("python"))print(sys.prefix == sys.base_prefix, "-> not in a virtual environment" if sys.prefix == sys.base_prefix else "-> in a virtual environment")Output
True True -> not in a virtual environment
Line by line
Line 5
Inside a virtual environment sys.prefix differs from sys.base_prefix.
Learn the concept
New to this topic? Read the lesson, then come back.