DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

Trying Poetry, another Python Dependency Manager

After tried Pyenv and Pipenv, I'm still not sure if it's really reliable and comfortable to use. Sometimes I got an error when installing package with Pipenv or got some issue when switching version using Pyenv. So maybe these tools is not stable enough (at least in my Window machine). Then I found this promising new dependency manager for Python named Poetry. By looking at the repository page on github and the documentation page, I'm convinced that this tool is more serious than previous two. So let's give it a try.

Installing Poetry on Windows Machine

Currently I'm using Python 3.8 on my machine. Based on installation docs, Installing Poetry is quite easy but make sure you have the required Python installed. Open Powershell console with Administrator privilege, use this script and enter:

(Invoke-WebRequest -Uri -UseBasicParsing).Content | python -
Enter fullscreen mode Exit fullscreen mode

Then it should do the work. Here is my log result:

C:\WINDOWS\system32> (Invoke-WebRequest -Uri -UseBasicParsing).Content | python -
Retrieving Poetry metadata

This installer is deprecated. Poetry versions installed using this script will not be able to use 'self update' command to upgrade to 1.2.0a1 or later.
# Welcome to Poetry!

This will download and install the latest version of Poetry,
a dependency and package manager for Python.

It will add the `poetry` command to Poetry's bin directory, located at:


This path will then be added to your `PATH` environment variable by
modifying the `HKEY_CURRENT_USER/Environment/PATH` registry key.

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing version: 1.1.7
  - Downloading poetry-1.1.7-win32.tar.gz (52.41MB)

Poetry (1.1.7) is installed now. Great!

To get started you need Poetry's bin directory (%USERPROFILE%\.poetry\bin) in your `PATH`
environment variable. Future applications will automatically have the
correct environment, but you may need to restart your current shell.

Enter fullscreen mode Exit fullscreen mode

Then open a new Command Prompt (CMD) console without administrator privilege and type only this and enter:

Enter fullscreen mode Exit fullscreen mode

It should verifies that Poetry is installed perfectly like this:

Poetry version 1.1.7

  poetry [-h] [-q] [-v [<...>]] [-V] [--ansi] [--no-ansi] [-n] <command> [<arg1>] ... [<argN>]

  <command>              The command to execute
  <arg>                  The arguments of the command

  -h (--help)            Display this help message
  -q (--quiet)           Do not output any message
  -v (--verbose)         Increase the verbosity of messages: "-v" for normal output, "-vv" for more verbose output and
                         "-vvv" for debug
  -V (--version)         Display this application version
  --ansi                 Force ANSI output
  --no-ansi              Disable ANSI output
  -n (--no-interaction)  Do not ask any interactive question

  about                  Shows information about Poetry.
  add                    Adds a new dependency to pyproject.toml.
  build                  Builds a package, as a tarball and a wheel by default.
  cache                  Interact with Poetry's cache
  check                  Checks the validity of the pyproject.toml file.
  config                 Manages configuration settings.
  debug                  Debug various elements of Poetry.
  env                    Interact with Poetry's project environments.
  export                 Exports the lock file to alternative formats.
  help                   Display the manual of a command
  init                   Creates a basic pyproject.toml file in the current directory.
  install                Installs the project dependencies.
  lock                   Locks the project dependencies.
  new                    Creates a new Python project at <path>.
  publish                Publishes a package to a remote repository.
  remove                 Removes a package from the project dependencies.
  run                    Runs a command in the appropriate environment.
  search                 Searches for packages on remote repositories.
  self                   Interact with Poetry directly.
  shell                  Spawns a shell within the virtual environment.
  show                   Shows information about packages.
  update                 Update the dependencies as according to the pyproject.toml file.
  version                Shows the version of the project or bumps it when a valid bump rule is provided.
Enter fullscreen mode Exit fullscreen mode

Creating Base Project using Poetry

Let's create a simple API project using FastAPI, starting to create the project base first with poetry:

poetry new fastapi-example
Enter fullscreen mode Exit fullscreen mode

it creates a new directory with this structure:

|__ fastapi_example
|__ tests
|__ pyproject.toml
|__ README.rst
Enter fullscreen mode Exit fullscreen mode

Installing Package using Poetry

It's recommended to create the virtual environment in the project itself. Set it with this command:

poetry config true
Enter fullscreen mode Exit fullscreen mode

The package needed for the project is obviously the fastapi package, to add it with poetry:

poetry add fastapi
Enter fullscreen mode Exit fullscreen mode

What are the changes after we add fastapi to the project? There are two files that affected: pyproject & poetry.lock. The changes are just like a normal dependency manager would look like, the installed package was added and listed.

Adding a package for the first will also creating the virtualenv directory. In my machine, the default location is in C:\Users\dendi\AppData\Local\pypoetry\Cache\virtualenvs.

To continue testing the fastapi project, we also need uvicorn for the server:

poetry add uvicorn[standard]
Enter fullscreen mode Exit fullscreen mode

Testing the FastAPI project using Poetry

Let's create file the root directory of project and fill it with the sample from official docs.

After that, let's run it with:

poetry run uvicorn main:app --reload
Enter fullscreen mode Exit fullscreen mode

The server should running and started, access it on browser at localhost:8000.

Is it running on your machine?

Top comments (2)

corentinbettiol profile image
Corentin Bettiol • Edited

Nice post!

Here are some commands that I use on a (nearly) daily basis:

I like to have my venvs in the same folder than my project, and so I use this config to update the default behavior of Poetry:

poetry config true
Enter fullscreen mode Exit fullscreen mode

For installing projects that uses poetry but that are not packages themselves, I use this command:

poetry install --no-root
Enter fullscreen mode Exit fullscreen mode

For deleting some annoying cache (in case poetry don't detect a new version of a package), I use:

poetry cache clear pypi
Enter fullscreen mode Exit fullscreen mode

For installing a package from github/gitlab (bonus if it's on a branch), I use:

poetry add git+
Enter fullscreen mode Exit fullscreen mode

For installing a particular version of a package (like django-treebeard, when the latest version broke django-cms), I use:

poetry add "django-treebeard>4.0,!=4.5.1"
Enter fullscreen mode Exit fullscreen mode
dendihandian profile image
Dendi Handian

I was looking for that in-project env config 😅thanks for these tips