Getting Started

Python Environment Setup

Learn how to install Python, set up your development environment, and run your first Python program on Windows, macOS, and Linux.

Python Environment Setup

Before you start writing Python code, you need to install Python and set up a proper development environment. This guide covers installation on all major operating systems and introduces essential tools.


Installing Python

Windows

  1. Visit the official Python website: python.org/downloads
  2. Download the latest Python 3.x installer
  3. Important: Check the box "Add Python to PATH" during installation
  4. Click "Install Now"

Verify the installation by opening Command Prompt:

bash
python --version
# Python 3.12.x

pip --version
# pip 24.x.x

macOS

macOS may come with Python 2 pre-installed, but you need Python 3:

bash
# Install using Homebrew (recommended)
brew install python

# Verify installation
python3 --version
# Python 3.12.x

Linux (Ubuntu/Debian)

Most Linux distributions come with Python 3 pre-installed:

bash
# Update package list
sudo apt update

# Install Python 3 and pip
sudo apt install python3 python3-pip

# Verify installation
python3 --version
pip3 --version

Choosing an IDE / Code Editor

EditorBest ForKey Features
VS CodeGeneral developmentFree, extensions, integrated terminal
PyCharmPython-specific developmentDebugging, refactoring, virtual envs
Jupyter NotebookData science, learningInteractive cells, inline output
IDLEAbsolute beginnersComes with Python, simple interface
Sublime TextLightweight editingFast, minimal, extensible

Recommended: Visual Studio Code with the Python extension is the best all-around choice for Python development.

VS Code Setup

  1. Download VS Code from code.visualstudio.com
  2. Install the Python extension by Microsoft
  3. Install the Pylance extension for enhanced IntelliSense
  4. Open a terminal in VS Code (Ctrl+`` ) and verify Python works

Running Python Code

Interactive Mode (REPL)

Open your terminal and type python (or python3 on macOS/Linux) to enter the interactive mode:

python
>>> print("Hello from REPL!")
Hello from REPL!

>>> 2 + 3
5

>>> name = "BigXStar"
>>> print(f"Welcome to {name}")
Welcome to BigXStar

>>> exit()

The REPL (Read-Eval-Print Loop) is great for quick experiments and testing small snippets.

Script Mode

Create a file called hello.py:

python
# hello.py
name = input("What is your name? ")
print(f"Hello, {name}! Welcome to Python.")

Run it from the terminal:

bash
python hello.py
# What is your name? Alice
# Hello, Alice! Welcome to Python.

Virtual Environments

Virtual environments let you isolate project dependencies so different projects can use different library versions.

Creating a Virtual Environment

bash
# Create a virtual environment
python -m venv myproject_env

# Activate it (Windows)
myproject_env\Scripts\activate

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

# Your prompt changes to show the active env
(myproject_env) $ python --version

Installing Packages with pip

bash
# Install a package
pip install requests

# Install a specific version
pip install flask==3.0.0

# Install from requirements file
pip install -r requirements.txt

# List installed packages
pip list

# Save current packages to requirements.txt
pip freeze > requirements.txt

Deactivating

bash
deactivate

Best Practice: Always use virtual environments for your Python projects to avoid dependency conflicts.


Project Structure

A typical Python project structure looks like this:

my_project/
├── venv/                  # Virtual environment (don't commit this)
├── src/
│   ├── __init__.py
│   ├── main.py
│   └── utils.py
├── tests/
│   ├── __init__.py
│   └── test_main.py
├── requirements.txt       # Project dependencies
├── README.md
└── .gitignore

Sample .gitignore for Python

venv/
__pycache__/
*.pyc
*.pyo
.env
.vscode/
*.egg-info/
dist/
build/

Verifying Your Setup

Create a file called setup_check.py and run it to verify everything is working:

python
# setup_check.py
import sys
import platform

print("=" * 40)
print("Python Environment Check")
print("=" * 40)
print(f"Python Version: {sys.version}")
print(f"Platform: {platform.system()} {platform.release()}")
print(f"Architecture: {platform.machine()}")
print(f"Python Path: {sys.executable}")
print("=" * 40)
print("✓ Your Python environment is ready!")

Expected output:

========================================
Python Environment Check
========================================
Python Version: 3.12.x (...)
Platform: Windows 11
Architecture: AMD64
Python Path: C:\Users\...\python.exe
========================================
✓ Your Python environment is ready!

Summary

  • Download and install Python 3 from python.org
  • Always check "Add Python to PATH" on Windows
  • Use VS Code with the Python extension for the best development experience
  • Use the REPL for quick experiments, script files for real programs
  • Always create virtual environments for project isolation
  • Use pip to install and manage packages

Next, we'll learn about Python's basic syntax and how to write clean Python code.