Which Python is running?

Easysys Module Programs · 16 of 20

Write a Python program that checks which interpreter file is running.

Advertisement
Output
True
True -> not in a virtual environment

Approach

  1. sys.executable.

Solution

Python
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.

More sys module programs

← All 20 sys Module Programs

Leave a Comment