Aller au contenu principal

00.2 - Setting Up Your Python Development Environment

Theory 20 min Beginner

Development Environment Overview

A Python development environment consists of three components:

┌─────────────────────────────────────────────────────────────┐
│ Python Dev Environment │
│ │
│ ┌──────────────┐ ┌─────────────────┐ ┌─────────────┐ │
│ │ Python 3 │ │ VS Code IDE │ │ Terminal │ │
│ │ Interpreter │ │ + Extensions │ │ (bash/zsh/ │ │
│ │ (CPython) │ │ │ │ PowerShell)│ │
│ └──────────────┘ └─────────────────┘ └─────────────┘ │
│ ▲ ▲ ▲ │
│ └───────────────────┴───────────────────┘ │
│ Your Python Project │
└─────────────────────────────────────────────────────────────┘

Step 1 — Install Python 3

Windows

  1. Go to https://www.python.org/downloads/
  2. Download the latest Python 3.x installer
  3. Important: Check ✅ "Add Python to PATH" before clicking Install
  4. Click "Install Now"

Verify:

python --version
# Python 3.11.x
pip --version
# pip 23.x from ...

macOS

# Using Homebrew (recommended)
brew install python3

python3 --version
pip3 --version

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install python3 python3-pip python3-venv

python3 --version

Step 2 — Install VS Code

  1. Download from https://code.visualstudio.com/
  2. Install the Python extension by Microsoft:
    • Open VS Code → Ctrl+Shift+X (Extensions)
    • Search "Python" → Install the Microsoft extension
ExtensionPurpose
Python (Microsoft)IntelliSense, debugging, linting
PylanceFast type checking and autocompletion
RuffUltra-fast linter (replaces flake8)
Black FormatterAuto-format code on save
JupyterRun notebooks inside VS Code
GitLensGit integration

Step 3 — Configure VS Code for Python

Press Ctrl+Shift+P and type "Python: Select Interpreter" to choose your Python version.

Add these to your VS Code settings.json (Ctrl+Shift+P → "Open User Settings JSON"):

{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
},
"python.analysis.typeCheckingMode": "basic",
"editor.rulers": [88]
}

Step 4 — The Python REPL

The REPL (Read-Eval-Print Loop) is the interactive Python shell — perfect for experimenting.

# Launch the REPL
# In terminal: python3

>>> print("Hello, World!")
Hello, World!

>>> 2 + 3
5

>>> name = "Python"
>>> f"Hello, {name}!"
'Hello, Python!'

>>> exit()
REPL commandPurpose
python3Start the REPL
exit() or Ctrl+DExit
_Last result (>>> 2+2 then >>> _ gives 4)
help(str)Get documentation on any object
dir(list)List all methods of an object

Step 5 — Your First Python Script

Create a file hello.py:

# hello.py — My first Python script

name = input("What is your name? ")
age = int(input("How old are you? "))

print(f"Hello, {name}!")
print(f"In 10 years, you will be {age + 10} years old.")

Run it:

python3 hello.py
# What is your name? Alice
# How old are you? 25
# Hello, Alice!
# In 10 years, you will be 35 years old.

Virtual Environments (Preview)

A virtual environment isolates your project's packages from the global Python installation. You'll learn the full details in Module 06, but here's a quick preview:

# Create a virtual environment
python3 -m venv venv

# Activate it (macOS/Linux)
source venv/bin/activate

# Activate it (Windows)
venv\Scripts\activate

# Install packages (isolated)
pip install requests

# Deactivate
deactivate

Rule: Always use a virtual environment for every project.


Python Code Structure

#!/usr/bin/env python3
"""
Module docstring: brief description of this file.
"""

# 1. Standard library imports
import os
import sys

# 2. Third-party imports
import requests

# 3. Local imports
# from mymodule import myfunction

# 4. Constants
MAX_RETRIES = 3

# 5. Functions and classes
def main():
"""Entry point of the script."""
print("Hello from main!")

# 6. Entry point guard
if __name__ == "__main__":
main()

The if __name__ == "__main__": guard ensures main() only runs when the script is executed directly, not when it's imported as a module.


Key Vocabulary

TermDefinition
InterpreterThe program that reads and executes Python code (python3)
REPLInteractive shell: type Python, get immediate results
ScriptA .py file containing Python code
IDEIntegrated Development Environment (VS Code, PyCharm)
ExtensionPlugin that adds functionality to VS Code
Virtual environmentIsolated Python installation for a single project
__name__Special variable: "__main__" when run directly, module name when imported
Shebang#!/usr/bin/env python3 — tells the OS which interpreter to use

Summary

  • Python 3.10+ is required — always add Python to PATH during installation
  • VS Code with the Python + Pylance + Black extensions is the recommended setup
  • The REPL (python3) lets you test code interactively
  • Use if __name__ == "__main__": as the entry point in every script
  • Virtual environments isolate project dependencies — always use them