Using Poetry to Manage Your Python Environments + Projects
I started using Poetry after my coworker recommended it when we started to build nppes. It’s completely changed the way I develop with Python.
Install + Use Poetry#
-
Install Poetry.
> curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
-
Check to see what version Poetry is.
> poetry --version
Start a new Python project with Poetry#
-
Head to the parent directory where you want the new project to live.
> poetry new project-name
This will create a directory called project-name with everything you need! The pyproject.toml
file has everything in it for your project’s dependencies.
-
Add some dependencies.
> poetry add pandas
This will add them to
pyproject.toml
so you don’t need to do that manually. It will also add and install all of the requirements for the package you just added. -
Remove a dependency.
> poetry remove pandas
-
Use Poetry to run Python. (This is like activating your virtual environment.) In your terminal, type
poetry run
before thepython
command.> poetry run python python-script.py