Python Usage#
This document provides a guide to setting up and using Python for development, focusing on version management with pyenv, and modern project/dependency management with tools like Poetry and venv.
Overview#
Python is a versatile, high-level programming language. Effective Python development involves managing Python versions and project dependencies robustly.
Python Version Management with pyenv#
It is highly recommended to use pyenv to manage multiple Python versions.
Installation of pyenv#
- Homebrew (macOS):
brew install pyenv - Other Systems/Manual Install: Follow the official
pyenvinstallation guide.
pyenv Initialization#
Add pyenv init to your shell configuration file (e.g., .zshrc), managed by chezmoi:
# Example for .zshrc
# if command -v pyenv 1>/dev/null 2>&1; then
# eval "$(pyenv init -)"
# eval "$(pyenv virtualenv-init -)" # If using pyenv-virtualenv (optional)
# fi
Installing Python Versions with pyenv#
- List available Python versions:
pyenv install --list - Install a Python version:
pyenv install <version>(e.g.,pyenv install 3.10.4) - Set Python version:
- Globally:
pyenv global <version> - Locally (per-project):
pyenv local <version>(creates.python-version) - Shell specific:
pyenv shell <version>
- Globally:
Project & Dependency Management#
Isolating project dependencies is crucial.
Option 1: Poetry (Recommended for New Projects)#
Poetry is a modern tool for Python dependency management and packaging. It helps you declare, manage, and install dependencies of Python projects, ensuring you have the right stack everywhere.
Key Benefits of Poetry:
- Dependency Resolution: Advanced resolver for compatible dependencies.
- Unified Tool: Manages project metadata, dependencies, virtual environments, building, and publishing.
pyproject.toml: Uses the standardpyproject.tomlfile to manage project information.- Lock File: Creates a
poetry.lockfile for deterministic builds. - Virtual Environment Management: Automatically creates and manages virtual environments for your projects.
Installation of Poetry:
Refer to the official Poetry installation guide. A common method is using their custom installer or pipx:
# Using pipx (recommended for CLI tools)
brew install pipx # If not already installed
pipx ensurepath
pipx install poetry
# Or using the official installer
# curl -sSL https://install.python-poetry.org | python3 -
Common Poetry Commands:
poetry new <project_name>: Creates a new Python project with a standard structure.poetry init: Interactively creates apyproject.tomlin an existing project.poetry install: Installs dependencies defined inpyproject.toml(andpoetry.lock). If a virtual environment doesn't exist, Poetry creates one.poetry add <package_name>: Adds a new dependency topyproject.tomland installs it.poetry add requestspoetry add pytest --group dev(for development dependencies)
poetry remove <package_name>: Removes a dependency.poetry show: Lists all installed packages.poetry update: Updates dependencies to their latest allowed versions according topyproject.tomland updatespoetry.lock.poetry run <command>: Runs a command within the project's virtual environment.poetry run python my_script.pypoetry run pytest
poetry shell: Activates the project's virtual environment in your current shell.poetry build: Builds your project into a source archive (sdist) and a wheel (bdist_wheel).poetry publish: Publishes your package to a repository like PyPI.
Poetry can be configured to use Python versions installed by pyenv:
poetry env use $(pyenv which python) # Or specify a path/version
Option 2: venv and pip (Traditional)#
Python 3.3+ includes the venv module for creating virtual environments.
- Create a virtual environment:
python -m venv .venv - Activate:
- Unix/macOS:
source .venv/bin/activate - Windows:
.\.venv\Scripts\activate.bator.\.venv\Scripts\Activate.ps1
- Unix/macOS:
- Deactivate:
deactivate
Package Management with pip (within an active venv):
- Install:
pip install <package_name>,pip install -r requirements.txt - List:
pip list - Generate
requirements.txt:pip freeze > requirements.txt
pyenv-virtualenv (Plugin for pyenv)#
The pyenv-virtualenv plugin integrates venv management with pyenv if you prefer this workflow over Poetry's built-in virtual environment handling.
Common Python Development Tools#
Install these within your project's virtual environment (via poetry add --group dev tool_name or pip install tool_name) or globally via pipx.
- Linters:
flake8,pylint - Formatters:
black,autopep8,isort - Test Runners:
pytest,unittest(built-in) pipx: For installing and running Python CLI applications in isolated environments (brew install pipx).- Example:
pipx install black
- Example:
Resources#
- Official Python Documentation: https://docs.python.org/3/
pyenvGitHub Repository: https://github.com/pyenv/pyenv- Poetry Documentation: https://python-poetry.org/docs/
- Python
venvDocumentation: https://docs.python.org/3/library/venv.html pipDocumentation: https://pip.pypa.io/- Python Packaging User Guide: https://packaging.python.org/
pipx: https://pipx.pypa.io/
This document covers essentials for a modern Python development setup. Choose the tools that best fit your project needs.