tags | aliases | cssclass | |
---|---|---|---|
💽 |
|
Poetry allows you to externally maintain Python packages and run scripts as you usually would using Makefiles.
- No more using
venv
orvirtualenv
directly! 🎉 - No manual virtual environment activation is required! ⭐
poetry run
takes care of this
- Gone are the days of requirements.txt files
- Package Management
- Dependency Resolution
- Version Locking
- Separation of Environments
- Dev vs. Prod Dependencies
- Install pipx
- Install poetry with
pipx install poetry
- If you have it installed then update with
pipx upgrade poetry
- If you have it installed then update with
# Creates pyproject.toml & poetry.lock files
# Note: It'll walk you through setting up the project details
poetry init
Note: If you only need poetry for dependency management and not package management, you can do the following:
- Add
package-mode = false
to the [tool.poetry] section of your pyproject.toml. This will makename
andversion
optional metadata.
# Set this before anything to ensure your virtual environment
# created is local in your project
poetry config virtualenvs.in-project true
# Add a package as dependency to your project
poetry add <package-name>
# Add pip package that are only used for development
poetry add <package-name> --dev
# Run a Python script
# (refer to '[tool.poetry.scripts]' section of your pyproject.toml file for specific functions)
poetry run lint
# Install packages and create an environment based on project specs
# Reads dependencies and versions from poetry.lock file
# Sets up and recreates a virtual environment
poetry install
# Anytime you update the pyproject.toml file you need to update the lock file
# You can do this with:
poetry lock --no-update
poetry install
reminds me of Javascriptnpm install
for installing node modules from a package.json file.poetry add
builds project's "blueprint" of dependencies.poetry install
constructs the actual working environment from that blueprint.
Specify Python Version: In your pyproject.toml file, you can indicate the range of Python versions your project supports:
[tool.poetry.dependencies]
python = "^3.8" # Supports 3.8 and newer
Virtual Environments per Python Version Poetry can automatically create virtual environments for different Python versions as needed.
Per-Environment Installation
To install your project into a specific environment, use the poetry env use
command. For example:
poetry env use python3.10
poetry install
🔗 Links to this page: [[Python]] [[virtualenv|venv]] [[pyenv]]