Skip to content

Python 3 - Environment Setup

Python 3 runs on Windows, macOS, and various Linux distributions. Before you start coding, you need to install Python and set up a development environment.

Python 2 reached its end-of-life in January 2020. This tutorial focuses exclusively on Python 3 (versions 3.6 and newer).

Python might already be installed on your system, especially on macOS and Linux. Open a terminal or command prompt and try these commands:

python --version
# or, especially on systems with both Python 2 and 3:
python3 --version

If you see a version number (e.g., Python 3.11.4), you have Python installed. If not, or if it’s an older version (like Python 2.7), you’ll need to install or update it.

The primary source for Python is the official website: https://www.python.org/downloads/. Download the latest stable version recommended for your operating system.

Download the recommended installer (usually an .exe file) from the Python website. During installation:

  • Crucially, check the box that says “Add Python X.Y to PATH” (where X.Y is the version number). This makes Python accessible from the command prompt.
  • Choose the “Install Now” option for default settings, or “Customize installation” for more control (e.g., changing the installation location).

After installation, close and reopen your command prompt and verify the installation using python --version.

Download the macOS installer (.pkg file) from the Python website. Double-click the file and follow the installation wizard. The installer usually handles PATH configuration automatically.

Alternatively, if you use the Homebrew package manager, you can install Python using:

brew install python3

Verify the installation in the Terminal with python3 --version.

Most modern Linux distributions come with Python 3 pre-installed. If not, or if you need a specific version, use your distribution’s package manager:

  • Debian/Ubuntu: sudo apt update && sudo apt install python3 python3-pip python3-venv
  • Fedora: sudo dnf install python3 python3-pip
  • CentOS/RHEL: sudo yum install python3 python3-pip (May require enabling specific repositories like EPEL on older versions)

Verify the installation with python3 --version. Note that on many Linux systems, python might still point to Python 2 (if installed), so explicitly use python3 and pip3.

It’s highly recommended to use virtual environments for your Python projects. A virtual environment is an isolated directory containing a specific Python interpreter and its own set of installed packages.

  • Dependency Management: Different projects might require different versions of the same library. Virtual environments prevent conflicts by keeping dependencies separate for each project.
  • System Cleanliness: Avoids installing packages globally, keeping your system Python installation clean.
  • Reproducibility: Makes it easy to recreate the project’s environment on another machine or for collaboration using a requirements.txt file.

Python 3 includes the venv module for creating virtual environments. Here’s a typical workflow:

# 1. Navigate to your project directory
cd my_project
# 2. Create a virtual environment (e.g., named '.venv')
# On Windows:
python -m venv .venv
# On macOS/Linux:
python3 -m venv .venv
# 3. Activate the virtual environment
# On Windows (Command Prompt):
.venv\Scripts\activate.bat
# On Windows (PowerShell):
.venv\Scripts\Activate.ps1
# On Windows (Git Bash):
source .venv/Scripts/activate
# On macOS/Linux:
source .venv/bin/activate
# Your prompt should now show (.venv) at the beginning
# 4. Install packages (they will be installed inside .venv)
pip install requests numpy pandas
# 5. Work on your project...
# 6. Deactivate the environment when done
deactivate

Remember to activate the environment each time you work on the project. The .venv directory should typically be added to your project’s .gitignore file.

pip is the standard package installer for Python. You use it to install libraries (packages) from the Python Package Index (PyPI) and other sources.

Common pip commands (run within an activated virtual environment):

  • pip install <package_name>: Install a package.
  • pip install -r requirements.txt: Install packages listed in a file.
  • pip list: List installed packages.
  • pip freeze > requirements.txt: Save installed packages and their versions to a file.
  • pip uninstall <package_name>: Remove a package.
  • pip install --upgrade <package_name>: Upgrade a package.
  • pip install --upgrade pip: Upgrade pip itself.

There are several ways to run Python code:

Type python or python3 in your terminal/command prompt. This opens the Python Read-Eval-Print Loop (REPL), where you can type commands and see results immediately. It’s great for testing small snippets.

>>> print("Hello from the interpreter!")
Hello from the interpreter!
>>> 2 + 2
4
>>> exit() # or Ctrl+Z then Enter (Windows), Ctrl+D (macOS/Linux)

Save your Python code in a file with a .py extension (e.g., my_script.py). Run it from the terminal using:

# Ensure you are in the directory containing the file
python my_script.py
# or
python3 my_script.py

On Linux/macOS, you can also make the script executable:

# Add this line at the very top of my_script.py:
#!/usr/bin/env python3
# Make the script executable:
chmod +x my_script.py
# Run it directly:
./my_script.py

3. Integrated Development Environments (IDEs) and Editors

Section titled “3. Integrated Development Environments (IDEs) and Editors”

IDEs provide comprehensive features like code editing, debugging, testing, and version control integration. Popular choices include:

  • Visual Studio Code (VS Code): Free, highly extensible editor with excellent Python support via extensions.
  • PyCharm: Powerful Python-specific IDE (Community edition is free, Professional edition is paid).
  • IDLE: Simple IDE included with Python installations. Good for beginners but lacks advanced features.
  • Jupyter Notebook/Lab: Web-based environments popular for data science, allowing code, text, and visualizations in one document.
  • Other editors like Sublime Text, Atom, Vim, Emacs also have strong Python support.

Most IDEs and modern editors allow you to run your Python scripts directly within the application.

Environment variables are system-wide settings. While venv reduces reliance on them, some are relevant:

  • PATH (or Path on Windows): Lists directories where the OS looks for executable programs. Python installers usually modify this (if you select the option) so you can run python from anywhere.
  • PYTHONPATH: Works like PATH but tells the Python interpreter where to find module files. Setting this manually is generally discouraged; prefer installing packages within virtual environments or structuring your project correctly.
  • PYTHONSTARTUP: Path to a Python script that runs automatically when the interactive interpreter starts.
  • PYTHONHOME: Alternative module search path; rarely needed for standard usage.

You typically don’t need to manually configure these variables for standard development, especially when using virtual environments.