Python Setup & Environment
Before writing your first line of Python, you need a working environment. This chapter walks you through installing Python 3 on any operating system, picking the right code editor, and setting up virtual environments — the professional way to manage projects.
Why Environment Setup Matters
Many beginners skip environment setup and jump straight to coding in an online editor. That works for small experiments, but the moment you build a real project, you need a proper local setup. Here's why it matters:
Installing Python 3
Python 3 is free and open-source. Always download it from the official website — python.org. Never install Python from unofficial sources. Select your operating system below:
# STEP 1 — Download the installer
# Visit https://www.python.org/downloads/
# Click the big yellow "Download Python 3.x.x" button
# STEP 2 — Run the installer
# ✅ IMPORTANT: Check "Add Python to PATH" at the bottom
# Then click "Install Now"
# STEP 3 — Verify in Command Prompt (Win + R → type cmd)
python --version
# Python 3.12.x
pip --version
# pip 24.x from C:\Users\...\site-packages\pip (python 3.12)
# STEP 4 — Open the interactive shell
python
# Python 3.12.x (tags/v3.12.x:...) on win32
# Type "help", "copyright", "credits" or "license" for more info.
# >>>
# OPTION A — Using Homebrew (recommended for developers)
# Install Homebrew first (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Python 3:
brew install python
# Verify:
python3 --version
# Python 3.12.x
pip3 --version
# pip 24.x
# OPTION B — Installer (easier for non-developers)
# Go to python.org/downloads → Download .pkg file → Run installer
# Note: On macOS use python3 and pip3 commands (not python/pip)
# Ubuntu / Debian
sudo apt update
sudo apt install python3 python3-pip python3-venv -y
# Verify:
python3 --version
# Python 3.12.x
pip3 --version
# pip 24.x
# Fedora / RHEL:
sudo dnf install python3 python3-pip -y
# Arch Linux:
sudo pacman -S python python-pip
# Create a convenient alias (add to ~/.bashrc):
echo 'alias python=python3' >> ~/.bashrc
source ~/.bashrc
python
command. If that happens, uninstall and reinstall with the checkbox ticked.
Verifying Your Installation
After installing Python, always verify it's working correctly before moving on. Open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and run these checks:
# Check Python version (Windows)
python --version
# Python 3.12.x ← must show 3.x, NOT 2.x
# Check Python version (macOS / Linux)
python3 --version
# Python 3.12.x
# Check pip (package installer)
pip --version # Windows
pip3 --version # Mac / Linux
# pip 24.x from ...site-packages/pip (python 3.12)
# Check Python's exact location
where python # Windows
which python3 # Mac / Linux
# C:\Users\YourName\AppData\Local\Programs\Python\Python312\python.exe
# Quick test in interactive shell
python # Windows (or python3 on Mac/Linux)
# >>>
print("Python is working!")
# Python is working!
exit() # quit the shell
Python 2.x, your system is pointing to
an old Python 2 install — follow Chapter 1's warning and make sure Python 3 is what's active.
Choosing a Code Editor
A code editor is where you write your Python files. You can technically write Python in Notepad, but a proper editor gives you syntax highlighting, auto-complete, debugging, and much more. Here are the top choices:
Setting Up VS Code for Python
VS Code is a free, lightweight editor from Microsoft. With the Python extension installed, it becomes a powerful Python IDE with IntelliSense, debugging, and linting. Here's the setup in 4 steps:
Ctrl+Shift+P (or Cmd+Shift+P on Mac) → type "Python: Select Interpreter"
→ choose the Python 3.x version you just installed. You'll see it listed with the full path.Ctrl+`). A terminal opens at the bottom. Type python --version to confirm everything is connected. Now you can run
Python files directly from VS Code.# Run current Python file
Ctrl+F5 # Run without debugging
F5 # Run with debugging (set breakpoints first)
# Terminal
Ctrl+` # Open/close integrated terminal
Ctrl+Shift+P # Open command palette (search any VS Code feature)
# Editor
Ctrl+/ # Toggle comment (add/remove # on selected lines)
Alt+Up/Down # Move line up or down
Ctrl+D # Select next occurrence of current word
Ctrl+Shift+F # Search across all files
Recommended VS Code Extensions
Virtual Environments — The Professional Way
A virtual environment is an isolated Python environment for each project. Without virtual environments, all your projects share the same global Python installation — and installing a package for one project could break another.
venv/ folder with its own Python
binary and pip installer, completely isolated from your system Python.pip install commands now install only into this environment.deactivate to return to your system
Python. The environment is preserved and can be activated again any time.# Navigate to your project folder first
cd my_project
# Create a virtual environment (named "venv" — standard convention)
python -m venv venv # Windows
python3 -m venv venv # macOS / Linux
# Activate the environment
venv\Scripts\activate # Windows (Command Prompt)
venv\Scripts\Activate.ps1 # Windows (PowerShell)
source venv/bin/activate # macOS / Linux
# Your prompt will now show: (venv) before it
# (venv) C:\my_project> ← Windows
# (venv) user@mac my_project % ← Mac/Linux
# Install packages (only goes into THIS environment)
pip install requests
pip install django
# See what's installed in this environment
pip list
# Package Version
# ------ -------
# requests 2.32.x
# django 5.x.x
# Save dependencies to a file (share with teammates)
pip freeze > requirements.txt
# Deactivate when done
deactivate
venv/ folder can be thousands of files. Never push it to Git. Instead,
commit requirements.txt so teammates can recreate the environment with
pip install -r requirements.txt.
pip — Python's Package Manager
pip (Package Installer for Python) lets you install, update, and remove packages from PyPI — the Python Package Index with 400,000+ free libraries. It comes bundled with Python 3 by default.
# Install a package
pip install requests
# Install a specific version
pip install django==4.2.0
# Install minimum version
pip install numpy>=1.24
# Upgrade a package to latest
pip install --upgrade requests
# Upgrade pip itself
python -m pip install --upgrade pip
# Uninstall a package
pip uninstall requests
# List all installed packages
pip list
# Show details about one package
pip show requests
# Name: requests
# Version: 2.32.3
# Summary: Python HTTP for Humans.
# Location: ...site-packages
# Install all packages from requirements.txt
pip install -r requirements.txt
# Search PyPI for packages
pip index versions django # see all available versions
Running Your First Python Script
Let's put everything together. You'll create a Python file, write a simple script, and run it from the terminal — just like a professional developer would.
my_first_project. Navigate into it. Then open VS Code in that folder.
hello.py. Write your script and save it with Ctrl+S.# 1. Create and enter project folder
mkdir my_first_project
cd my_first_project
# 2. Create virtual environment
python -m venv venv # Windows
python3 -m venv venv # macOS / Linux
# 3. Activate it
venv\Scripts\activate # Windows
source venv/bin/activate # macOS / Linux
# 4. Open VS Code in this folder
code .
# 5. In VS Code, create hello.py with this content:
print("Hello from my first Python project!")
name = input("Enter your name: ")
print(f"Welcome to Python, {name}! 🐍")
# 6. Run from terminal
python hello.py # Windows
python3 hello.py # macOS / Linux
# Hello from my first Python project!
# Enter your name: Alice
# Welcome to Python, Alice! 🐍
# hello.py — My first Python script
# Run with: python hello.py (Windows) or python3 hello.py (Mac/Linux)
print("╔══════════════════════════════╗")
print("║ My First Python Script! 🐍 ║")
print("╚══════════════════════════════╝")
print()
name = input("What is your name? ")
age = input("How old are you? ")
print(f"\nHello, {name}! You are {age} years old.")
print(f"In 5 years you'll be {int(age) + 5} years old.")
print("\nYour environment is perfectly set up! ✅")
Setup Checklist
python --versionpip --version- Always install Python from python.org — never from unofficial sources. Always download Python 3.
- On Windows, always check "Add Python to PATH" during installation, or the
pythoncommand won't work in the terminal. - Verify with
python --version(Windows) orpython3 --version(Mac/Linux). You should seePython 3.x.x. - VS Code with the Microsoft Python extension is the recommended editor — free, fast, and professional.
- A virtual environment (
python -m venv venv) isolates each project's packages. Always activate before installing. - pip is your package manager. Use
pip install Xto add libraries, andpip freeze > requirements.txtto save dependencies. - Run a Python script with
python filename.pyfrom the terminal inside VS Code or any terminal. - Always add
venv/to your.gitignorefile — never push virtual environments to Git.
The PATH environment variable tells Windows where to find executable programs. When
you type python in the terminal, Windows searches every folder
listed in PATH. If Python's folder isn't in PATH, Windows says "python is not recognized" even if
Python is installed. Checking the box during installation automatically adds Python's directory to
PATH so you can type python from any folder in any terminal.
A virtual environment is an isolated Python installation for a single project.
Without it, all projects share one global Python, so installing Django 4.2 for Project B might break
Project A which needs Django 3.2. A virtual environment gives each project its own site-packages directory, its own pip, and its own Python binary —
completely independent from the system Python and from every other project.
pip install requests installs one specific
package. pip install -r requirements.txt reads a text
file and installs every package listed in it with their exact versions. The requirements.txt file is generated by pip freeze > requirements.txt. This is the standard way to share
project dependencies — a teammate clones your repo, runs pip install -r requirements.txt, and has the exact same environment.
Older macOS versions shipped with Python 2 pre-installed as the system Python (the
python command). To avoid breaking system tools that still depend
on Python 2, Apple and the Python community decided that the python3 command should refer specifically to Python 3. Using python3 and pip3 ensures you're
always using the correct, modern version. On newer macOS (Monterey+), Python 2 is no longer
pre-installed, but the convention of using python3 remains.
The venv/ folder contains thousands of binary files compiled
specifically for your machine's OS and architecture. It can be hundreds of megabytes.
Another developer on a different OS can't use your venv binaries — they need to create their own. The
correct approach: commit only requirements.txt, and add venv/ to .gitignore. Each
developer runs python -m venv venv and pip install -r requirements.txt to recreate the environment locally.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.