Python 3 - Environment Setup
Python 3 - Environment Setup
Section titled “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).
Checking Your Installation
Section titled “Checking Your Installation”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 --versionIf 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.
Installing Python
Section titled “Installing Python”The primary source for Python is the official website: https://www.python.org/downloads/. Download the latest stable version recommended for your operating system.
Windows
Section titled “Windows”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 python3Verify 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.
Virtual Environments: The Best Practice
Section titled “Virtual Environments: The Best Practice”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.
Why use virtual environments?
Section titled “Why use virtual environments?”- 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.txtfile.
Using venv (Built-in)
Section titled “Using venv (Built-in)”Python 3 includes the venv module for creating virtual environments. Here’s a typical workflow:
# 1. Navigate to your project directorycd 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 donedeactivateRemember to activate the environment each time you work on the project. The .venv directory should typically be added to your project’s .gitignore file.
Managing Packages with pip
Section titled “Managing Packages with pip”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.
Running Python Code
Section titled “Running Python Code”There are several ways to run Python code:
1. Interactive Interpreter (REPL)
Section titled “1. Interactive Interpreter (REPL)”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 + 24>>> exit() # or Ctrl+Z then Enter (Windows), Ctrl+D (macOS/Linux)2. Running Script Files
Section titled “2. Running Script Files”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 filepython my_script.py# orpython3 my_script.pyOn 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.py3. 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 (Brief Overview)
Section titled “Environment Variables (Brief Overview)”Environment variables are system-wide settings. While venv reduces reliance on them, some are relevant:
PATH(orPathon Windows): Lists directories where the OS looks for executable programs. Python installers usually modify this (if you select the option) so you can runpythonfrom anywhere.PYTHONPATH: Works likePATHbut 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.