Tutorials Python Setup & Environment
Ch 2 / 58
Python Beginner 7 min read Chapter 2 of 58

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.

What you'll learn in this chapter
Install Python 3 on Windows, macOS & Linux
Understand what PATH is and why it matters
Choose and configure the best code editor
Create and activate a virtual environment
Install packages with pip
Run your first .py script from the terminal
Advertisement

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:

Full Speed
No internet lag, runs locally
Real Packages
Install any library with pip
Full Debugger
Set breakpoints, inspect vars
File System
Read/write actual files
Good news The entire setup takes about 10–15 minutes. Once done, you won't have to do it again. Every professional Python developer uses this exact workflow.

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:

Windows — Command Prompt / PowerShell
# 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.
# >>>
macOS — Terminal (Homebrew recommended)
# 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)
Linux — Terminal (Ubuntu / Debian)
# 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
Windows gotcha The most common mistake on Windows is forgetting to check "Add Python to PATH" during installation. If you miss this, your terminal won't find the 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:

Terminal — Verification commands
# 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
🗺️ What is PATH?
PATH is a system variable that tells your OS where to look for executable programs. When you type python in the terminal, the OS searches every folder listed in PATH until it finds the Python executable. Without Python in PATH, you'd have to type the full path like C:\Python312\python.exe every single time — very inconvenient.
Both commands show 3.x? You're perfectly set up! If you see 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.
Advertisement

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:

PyCharm
JetBrains IDE
Purpose-built for Python. Powerful refactoring, smart debugging, and Django support. Community edition is free.
✓ Community Free
Jupyter Notebook
Data Science
Run code in cells, see output inline. The go-to choice for data science, ML, and teaching Python interactively.
✓ Free
Thonny
Best for Beginners
Comes with Python built-in. Simple debugger shows exactly how Python executes your code step by step — perfect for learning.
✓ Free
Our recommendation Use VS Code for this tutorial — it's what professionals use, it's free, and the setup takes under 3 minutes. We'll walk through it in the next section.

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:

1
Download & Install VS Code
Go to code.visualstudio.com and download the installer for your OS. Run the installer — accept all defaults. On Windows, check "Add to PATH" and "Register Code as an editor for supported file types".
2
Install the Python Extension
Open VS Code → click the Extensions icon (left sidebar, looks like four squares) → search "Python" → install the one by Microsoft (it has millions of downloads). This gives you IntelliSense, linting, debugging, and Jupyter support.
3
Select the Python Interpreter
Press 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.
4
Open Terminal Inside VS Code
Go to Terminal → New Terminal (or press 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.
VS Code — Useful keyboard shortcuts
# 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

Python (Microsoft)
The essential extension — IntelliSense, linting with Pylint/Flake8, debugging, and Jupyter notebook support all-in-one.
Pylance
Super-fast type checking and auto-complete. Works alongside the Python extension for much smarter code suggestions.
Black Formatter
Auto-formats your code to PEP 8 style on every save. Keeps your code clean without thinking about formatting.
GitLens
See Git history, blame annotations, and compare file changes. Essential once you start using version control.
Advertisement

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.

Real-world analogy Think of a virtual environment like a separate toolbox for each project. Project A needs Django 3.2, Project B needs Django 4.2 — they can't both live in the same box. Virtual environments solve this perfectly.
1
Create the environment
Generates a venv/ folder with its own Python binary and pip installer, completely isolated from your system Python.
2
Activate it
Once activated, your terminal prompt shows the env name. All pip install commands now install only into this environment.
3
Install packages inside
Install exactly what this project needs. Other projects are completely unaffected — no version conflicts ever again.
4
Deactivate when done
Type deactivate to return to your system Python. The environment is preserved and can be activated again any time.
Terminal — Creating & using a virtual environment
# 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
Always add venv/ to .gitignore The 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.

Terminal — Essential pip commands
# 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
pip install X
Downloads and installs package X and all its dependencies from PyPI automatically.
pip freeze
Prints all installed packages with exact versions. Redirect to a file to create a reproducible dependency list.
pip install -r requirements.txt
Installs every package listed in the requirements file. The standard way to share project dependencies with a team.
pip install --upgrade pip
Always keep pip itself up to date. Old pip versions sometimes have bugs resolving package dependencies.
K2infocom Python Masterclass & Backend Development
Master Python, Django & Backend APIs with AI
Go from absolute beginner to building production-ready REST APIs, web applications, and ML pipelines. Real projects, live mentoring in Vadodara, and placement support included.
2,400+
Students Enrolled
4.9
Google Rating
6 Months
Full Program
100%
Placement Support
Python 3 Django & DRF FastAPI PostgreSQL Machine Learning Docker & AWS Real Projects AI Tools

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.

1
Create a project folder
Open your terminal and create a folder called my_first_project. Navigate into it. Then open VS Code in that folder.
2
Create a virtual environment
Always start a new project by creating a virtual environment. Activate it so all packages install locally.
3
Create hello.py
In VS Code, create a new file called hello.py. Write your script and save it with Ctrl+S.
4
Run it from terminal
In the VS Code terminal, type the run command. You should see your output printed immediately.
Terminal — Full workflow from scratch
# 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! 🐍
Python — hello.py (the actual file content)
# 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 3.x installed — confirmed with python --version
✓
pip working — confirmed with pip --version
✓
VS Code installed with the Python extension by Microsoft
✓
Virtual environment created and activated in your project folder
✓
hello.py runs successfully and prints output to the terminal
Chapter Summary
  • 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 python command won't work in the terminal.
  • Verify with python --version (Windows) or python3 --version (Mac/Linux). You should see Python 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 X to add libraries, and pip freeze > requirements.txt to save dependencies.
  • Run a Python script with python filename.py from the terminal inside VS Code or any terminal.
  • Always add venv/ to your .gitignore file — never push virtual environments to Git.
⚡
Free Resource
Download our Python Cheat Sheet
Variables, data types, loops, functions, OOP — all on one printable page.
Get Sheet
Advertisement
Quick Check — Test Your Understanding
Question & Answer
✓ Answer

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.

✓ Answer

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.

✓ Answer

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.

✓ Answer

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.

✓ Answer

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.

MCQ 1 of 3
Which command creates a virtual environment in Python 3?
MCQ 2 of 3
What command saves all installed packages to a file so others can reproduce your environment?
MCQ 3 of 3
On Windows, how do you activate a virtual environment named "venv"?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel