Python Installation and Setup

Install Python 3, create a virtual environment, manage packages with pip, and configure VS Code for Python development.

Beginner 8 min read 7 examples

Install Python

Windows
# Option 1: Microsoft Store (easiest)
# Search "Python 3.12" in Microsoft Store and install

# Option 2: Official installer from python.org
# Download python-3.12.x-amd64.exe
# IMPORTANT: check "Add Python to PATH" during install

# Option 3: winget (package manager)
winget install Python.Python.3.12

# Option 4: Chocolatey
choco install python
macOS
# Option 1: Homebrew (recommended)
brew install python@3.12

# Option 2: Official installer
# Download from python.org/downloads/mac-osx/
# Installs to /Library/Frameworks/Python.framework/

# Option 3: pyenv (best for managing multiple versions)
brew install pyenv
pyenv install 3.12.3
pyenv global 3.12.3   # set as default
Linux
# Ubuntu / Debian
sudo apt update
sudo apt install python3.12 python3.12-venv python3-pip

# Fedora / RHEL
sudo dnf install python3.12

# From source (any Linux - gives latest version)
sudo apt install build-essential libssl-dev zlib1g-dev
wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz
tar xf Python-3.12.3.tgz && cd Python-3.12.3
./configure --enable-optimizations
make -j$(nproc) && sudo make altinstall

Verify Installation

Shell
# Check Python version
python3 --version      # Python 3.12.3
python --version       # may be Python 2.x on some systems
py --version           # Windows py launcher

# Check pip
python3 -m pip --version   # pip 24.0 from ...

# Check where Python is installed
which python3          # /usr/local/bin/python3 (macOS/Linux)
where python           # C:\Python312\python.exe (Windows)

# Quick test in terminal
python3 -c "print('Python is working!')"

Virtual Environments

Always create a virtual environment before installing packages for a project. This keeps project dependencies isolated.

Shell
# Create a virtual environment in a folder called .venv
python3 -m venv .venv

# Activate it
source .venv/bin/activate      # macOS / Linux
.venv\Scripts\activate         # Windows PowerShell
.venv\Scripts\activate.bat     # Windows CMD

# Prompt changes to show the active venv:
# (.venv) alice@machine:~/project$

# Now install packages - they go into .venv only
pip install requests pandas

# See what is installed
pip list
pip freeze > requirements.txt   # save exact versions

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

# Deactivate when done
deactivate
Add .venv to .gitignore

Never commit your .venv folder to git - it is large, OS-specific, and reproducible from requirements.txt. Add .venv/ to your .gitignore. Commit requirements.txt (or pyproject.toml) instead so teammates can recreate the environment.

pip Package Manager

Shell
# Install a package
pip install requests
pip install requests==2.31.0     # specific version
pip install "requests>=2.28"     # minimum version

# Install multiple packages
pip install fastapi uvicorn pydantic

# Upgrade a package
pip install --upgrade requests

# Uninstall
pip uninstall requests

# Search PyPI (or visit pypi.org)
pip search requests    # deprecated; use pypi.org instead

# Show package info and location
pip show requests

# List outdated packages
pip list --outdated

# Use python -m pip to ensure the right Python
python3 -m pip install requests   # safer than bare 'pip'

VS Code Setup

Shell
# Install VS Code extensions (from terminal)
code --install-extension ms-python.python          # Python extension (official)
code --install-extension ms-python.pylance         # Type checking + autocomplete
code --install-extension ms-python.black-formatter # Auto-formatting

# Or install from VS Code Extensions panel (Ctrl+Shift+X)
# Search: "Python" by Microsoft

# Select interpreter in VS Code:
# Ctrl+Shift+P -> "Python: Select Interpreter"
# Choose your .venv Python (e.g., ./.venv/bin/python)

# settings.json additions for Python
# "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"
# "[python]": { "editor.formatOnSave": true, "editor.defaultFormatter": "ms-python.black-formatter" }
Alternatives to VS Code

PyCharm (JetBrains) is the most feature-rich Python IDE - excellent for larger projects with built-in debugger, database tools, and refactoring. The Community edition is free. Jupyter Notebooks (via Jupyter or VS Code) are standard for data science and ML work. IDLE ships with Python and is fine for learning basics.

The Python REPL

The REPL (Read-Eval-Print Loop) lets you run Python interactively. Use it for quick experiments, testing ideas, and exploring objects.

Python REPL
$ python3
Python 3.12.3 (main, Apr  9 2024, 08:09:14)
>>> 2 + 2
4
>>> name = "Alice"
>>> f"Hello, {name}!"
'Hello, Alice!'

>>> # Explore an object's methods
>>> dir([])
['append', 'clear', 'copy', 'count', ...]

>>> # Get help on any object
>>> help(str.split)

>>> # Multi-line input - use ... continuation
>>> def add(a, b):
...     return a + b
...
>>> add(3, 4)
7

>>> exit()   # or Ctrl+D (macOS/Linux), Ctrl+Z Enter (Windows)

For a better REPL experience, install IPython: pip install ipython. It adds syntax highlighting, auto-completion, magic commands (%timeit, %run), and history search.

Frequently Asked Questions

On macOS and Linux, python3 is the safe choice - many systems still have Python 2 as python. On Windows, if you installed Python 3 from python.org, both python and py should work. The py launcher on Windows is the most reliable option. Check with python --version and python3 --version to see what each points to.

Virtual environments isolate project dependencies. Without them, all projects share the same global Python packages, causing version conflicts - Project A needs requests 2.28 but Project B needs 2.31. With a venv, each project has its own packages directory. Always create a venv for each project before installing packages. It also makes it easy to share your project: pip freeze > requirements.txt captures exact versions.

On systems with both Python 2 and Python 3, pip may point to Python 2 and pip3 to Python 3. Inside a virtual environment, pip always installs to that environment regardless of version. The safest approach is to always use python -m pip install - this guarantees pip uses the same Python interpreter you are currently working with.

pyenv lets you install and switch between multiple Python versions on the same machine (e.g., 3.10, 3.11, 3.12 side by side). You need it when different projects require different Python versions. It is especially common on macOS/Linux for development. On Windows, the py launcher provides similar functionality. Install pyenv from github.com/pyenv/pyenv.