To begin development with Python, you need a working Python 3 runtime and an isolated project environment. Managing dependencies cleanly from day one prevents package collisions across projects.
flowchart TD
A["Install Python 3 Runtime"] --> B["Create Virtual Environment (venv)"]
B --> C["Activate Virtual Environment"]
C --> D["Install Packages via pip"]
D --> E["Execute Script via Terminal"]
Always create a local venv directory inside your project root to isolate installed packages:
# Initialize virtual environment
python -m venv .venv
# Activate on Linux / macOS
source .venv/bin/activate
# Activate on Windows (PowerShell)
.venv\Scripts\Activate.ps1
import sys
from pathlib import Path
def verify_environment() -> None:
"""Check if the script is running inside a virtual environment."""
executable_path = Path(sys.executable)
in_venv = sys.prefix != sys.base_prefix
print(f"Python Interpreter Path: {executable_path}")
print(f"Virtual Environment Active: {in_venv}")
if not in_venv:
print("Warning: Running globally! Consider activating a virtual environment (.venv).")
if __name__ == "__main__":
verify_environment()
pip install outside an active virtual environment to keep your system Python clean..gitignore for .venv: Never check .venv or __pycache__ directories into version control.if __name__ == '__main__': to ensure modules can be imported safely without executing side effects.Create a virtual environment, activate it, write a Python script named check_env.py that verifies sys.prefix, and verify that in_venv evaluates to True.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With