E&K Labs
Published in

E&K Labs

Try the Python dependency manager Poetry

While writing Reasons to Use Pycharm (link), I found out that there is a dependency manager other than pip.

It is a package manager similar to Cargo of Rust language and NPM of Node.js.

In the meantime, I did not feel any inconvenience while using pip, so I did not think to find another package manager. Poetry provides some of the features that pip lacks.

In this article, we are going to briefly learn what Poetry is and try to use it.

What is Poetry?

Like pip, it is a dependency manager that manages and packages dependencies.

In addition, it is possible to directly manage the virtual environment as a global application.

The great thing about poetry is that it allows you to log information and dependencies of packages through pyproject.toml and is itself a tool for managing multiple environments.

Of course, there is also the venv module, but creating it in a separate cache directory instead of creating it within the project means that when uploading to git, there is no need to add it to gitignore, and it can be managed with a single tool without a separate tool such as pyenv in a Linux environment.

Also, it helps to develop a Python package by creating a whl file or tar.gz with a simple command and deploying it to pypi.

Install Poetry

It is installed through a simple script code.

curl -sSL <https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py> | python -

For Windows, a PowerShell script is also provided.

(Invoke-WebRequest -Uri <https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py> -UseBasicParsing).Content | python -

If it works when you enter the following command, the installation is complete.

poetry --version# Poetry version 1.1.13

Basic usage of Poetry

Create new project

poetry new poetry-demo

poetry-demo is the name of the new project you want to create.

Initialize existed project

cd pre-existing-project
poetry init

Run project

poetry run python your_script.py

If you are in a specific folder, you also need to specify a relative path.
When poetry run is used, it runs using the virtual environment created in the cache directory.

Install dependancy

To install the specified dependency library in pyproject, you can use the install command. (Same principle as npm install.)

poetry install

Update dependancy

Update the list of dependencies that are already registered in pyproject.toml or poetry.lock.

poetry update

Add new dependancy

Unlike npm, you add new dependencies with add command rather than install.

poetry add new-library

Remove dependancy

poetry remove new-library

Show dependancies

poetry show

Export dependancies

It exports the file in a different format such as the requirements.txt.

poetry export -f requirements.txt > requirements.txt

Manage env

Rather than directly installing and creating a virtual environment, Poetry is similar to creating a new virtual environment using multiple versions that have already been created.

You can regist and remove the base interpreter to use with the commands below.

# Indicate which version of Python to use
poetry env use /path/to/python3
# Registered Env List
poetry env list
# Delete registered env
poetry env remove /path/to/python3

Setting up a Django project using Poetry

For the exercise, let’s create and run a Django application.

# Create new poetry project
poetry new djangoProject
cd djangoProject
# Add a dependancy
poetry add django
# Create Django project
poetry run django-admin startproject demoproject .
# Add new app
poetry run python manage.py startapp poll
# Run the server
poetry run python manage.py runserver

Poetry pros and cons


- It is good to list the dependencies that I installed. (In the case of PIP, when extracting requirements.txt, additional dependencies are included in addition to the libraries I added, making it difficult to see and manage.)
- Optimized for Pypi deployments
- Virtual environment does not exist inside the project
- Supported by PyCharm


- A separate cache directory exists (I think this is an advantage, but it can also be a disadvantage)
- It can be still unstable.
- An unknown error occurred in environment management (There was a difficulty in registering a new environment in an environment using asdf-vm.)


I haven’t used it in a deployment yet so I don’t understand Poetry exactly.

But, it seems to be a package manager that can be expected as the things that felt regretful in pip have been resolved.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store