Install Python, set up environment for productivity
The official installation instructions for Python are available here.
Python Environment
Python Version
This book uses Python 3.x (Python 3.8 or later is recommended). Python 2 reached end-of-life in January 2020 and should not be used for new projects.
You can check your Python version by running:
python --version
# or
python3 --version
Virtual Environments
Python's virtual environments allow you to isolate project dependencies. This is crucial for maintaining clean, reproducible builds.
Create a new virtual environment for your project:
# Create a new directory for your project
mkdir my-project
cd my-project
# Create a virtual environment
python -m venv venv
# Activate the virtual environment
# On macOS/Linux:
source venv/bin/activate
# On Windows:
# venv\Scripts\activate
When activated, your terminal prompt will show (venv) to indicate you're working in the virtual environment.
Package Management
Python uses pip for package management. With an active virtual environment, you can install packages:
pip install pytest
To save your dependencies:
pip freeze > requirements.txt
To install dependencies from a requirements file:
pip install -r requirements.txt
Testing with pytest
While Python has a built-in testing framework (unittest), we'll use pytest for this book. It's more pythonic and has better output:
pip install pytest
To run tests:
pytest
For more verbose output:
pytest -v
Python Linting
Good code style is important. We recommend using the following tools:
Black - Code Formatter
Black is an opinionated code formatter that enforces a consistent style:
pip install black
black .
Ruff - Linter
Ruff is a fast Python linter that can catch many common issues:
pip install ruff
ruff check .
Type Checking with mypy
Python supports optional type hints. mypy can check these for you:
pip install mypy
mypy .
Refactoring and your tooling
A big emphasis of this book is the importance of refactoring.
Your tools can help you do bigger refactoring with confidence.
You should be familiar enough with your editor to perform the following with a simple key combination:
- Extract/Inline variable. Taking magic values and giving them a name lets you simplify your code quickly.
- Extract method/function. It is vital to be able to take a section of code and extract functions/methods.
- Rename. You should be able to rename symbols across files confidently.
- Auto-format. Your editor should run a formatter (like Black) on every file saved.
- Run tests. You should be able to do any of the above and then quickly re-run your tests to ensure your refactoring hasn't broken anything.
In addition, to help you work with your code, you should be able to:
- View function signature. You should never be unsure how to call a function in Python. Your IDE should describe a function in terms of its documentation, its parameters and what it returns.
- View function definition. If it's still unclear what a function does, you should be able to jump to the source code and try and figure it out yourself.
- Find usages of a symbol. Understanding a function's context can help you make decisions when refactoring.
Mastering your tools will help you concentrate on the code and reduce context switching.
Recommended IDE Setup
VS Code
VS Code with the Python extension is an excellent choice:
- Install VS Code
- Install the Python extension
- Install the Pylance extension for enhanced Python support
PyCharm
PyCharm is a full-featured Python IDE with excellent refactoring support. The Community edition is free and sufficient for most projects.
Wrapping up
At this point, you should have:
- Python 3.x installed
- Know how to create and activate virtual environments
- pytest installed for testing
- A text editor configured for Python development
Python has a very large ecosystem of third-party packages. For a more complete list of awesome Python resources, see Awesome Python.