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 the look of its repository 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 https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -

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

C:\WINDOWS\system32> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -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:

%USERPROFILE%\.poetry\bin

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.

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

poetry

It should verifies that Poetry is installed perfectly like this:

C:\Users\dendi>poetry
Poetry version 1.1.7

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

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

GLOBAL OPTIONS
  -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

AVAILABLE COMMANDS
  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.

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

it creates a new directory with this structure:

fastapi-example
|__ fastapi_example
    |__ __init__.py
|__ tests
    |__ __init__.py
    |__ test_fastapi_example.py
|__ pyproject.toml
|__ README.rst

Installing Package using Poetry

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

poetry config virtualenvs.in-project true

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

poetry add fastapi

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]

Testing the FastAPI project using Poetry

Let's create main.py 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

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

Is it running on your machine?

17