Appendix A — Setting Up Python
This appendix covers how to install Python, choose an execution environment, and set up your development tools.
A.1 Interpreted vs. Compiled Languages
Python is an interpreted language, your code is executed line by line rather than being compiled into machine code first. This makes Python excellent for learning and prototyping because you get immediate feedback.
A.2 Ways to Run Python Code
A.2.1 The Python Interpreter
The simplest way to run Python is the interactive interpreter. Open a terminal and type:
python3You will see a prompt (>>>) where you can type Python statements and see results immediately:
>>> 2 + 2
4
>>> print("Hello!")
Hello!
>>> exit()
A.2.2 IPython
IPython is an enhanced interactive shell with features like tab completion, syntax highlighting, and history search:
pip install ipython
ipythonUseful IPython features:
- Tab completion: type the start of a name and press Tab
?: append to any object for documentation (print?)%timeit: measure execution time of a statement
A.2.3 Python Scripts
For larger programs, save your code in .py files and run them:
python3 my_script.pyOn macOS/Linux, you can make scripts directly executable:
#!/usr/bin/env python3
"""My first script."""
print("Hello from a script!")chmod +x my_script.py
./my_script.pyA.2.4 Jupyter Notebooks
Jupyter Notebooks combine code, output, and documentation in one file. They are ideal for learning, data analysis, and experimentation:
pip install jupyter
jupyter notebookThis opens a browser-based interface where you can create and run notebooks.
A.2.5 Choosing the Right Environment
| Environment | Best For |
|---|---|
| Interpreter | Quick calculations, testing ideas |
| IPython | Interactive exploration, debugging |
| Scripts | Reusable programs, automation |
| Jupyter | Learning, data analysis, sharing |
A.3 Installing Python
A.3.1 Recommended: Miniconda
Miniconda provides Python plus the conda package manager in a lightweight install:
- Download from docs.conda.io/en/latest/miniconda.html
- Run the installer and follow the prompts
- Verify the installation:
python --version
conda --versionA.3.2 Alternative: Official Python
Download from python.org if you prefer a minimal install without conda.
A.3.3 Managing Packages with pip
pip is Python’s standard package installer:
# Install a package
pip install requests
# Install a specific version
pip install pandas==2.0.0
# Install from a requirements file
pip install -r requirements.txt
# List installed packages
pip listA.3.4 Virtual Environments
Virtual environments isolate each project’s dependencies:
# Create a virtual environment
python3 -m venv .venv
# Activate it
# macOS/Linux:
source .venv/bin/activate
# Windows:
.venv\Scripts\activate
# Install packages (isolated to this environment)
pip install pytest
# Deactivate when done
deactivateAlways use a virtual environment for each project. Add .venv/ to your .gitignore file.
A.4 Useful Libraries by Domain
| Domain | Libraries |
|---|---|
| Data Science | pandas, numpy, matplotlib |
| Web Development | Flask, Django, FastAPI |
| Automation | requests, beautifulsoup4, selenium |
| Testing | pytest, coverage |
Install what you need as you go, do not install everything at once.
A.5 Setting Up an IDE
VS Code is recommended for beginners:
- Install from code.visualstudio.com
- Install the Python extension
- Open your project folder
- Select your Python interpreter (bottom status bar)
PyCharm is a full-featured alternative with built-in Python support.
Jupyter Lab extends Jupyter Notebooks with a more complete IDE-like interface:
pip install jupyterlab
jupyter labA.6 Troubleshooting
“command not found” errors:
# macOS/Linux: add to your PATH
echo 'export PATH="$HOME/miniconda3/bin:$PATH"' \
>> ~/.bashrc
source ~/.bashrc
# Windows (PowerShell as administrator):
$conda = "C:\Users\YourUsername\miniconda3"
$Env:Path = "$Env:Path;$conda;$conda\Scripts"Package conflicts: Use virtual environments to isolate projects.
Permission errors:
# macOS/Linux
sudo chown -R $USER:$USER ~/miniconda3Mixed Python versions: Use python3 explicitly rather than python to ensure you are using the correct version.