top of page
BlueDolphin

Python Poetry - Packaging and dependency management made easy



References:

https://github.com/python-poetry/poetry


Summary

Managing packages and dependencies in Python can be overwhelming and resource intensive. Poetry will help with starting new projects, maintaining existing ones and provide centralized dependency management. Like most Python projects, it relies on external packages. The maintenance work falls on the developer to make sure each version of each package is updated and working after each update. By leveraging Poetry, a dependency manager, we can specify, install and resolve external packages in one source location known as out project file. This way we can ensure we always have the right dependencies,lol versions and configurations, and dependencies of dependencies, with the added value of central management.


Files and more files

Typically with a python project we have to track setup.py, requirements.txt, setup.cfg, MANIFEST.in and a pipfile. Let's take a look at them below.


setup.py - Is a python file that indicates the modules/packages you are about to install. Simply calling "pip install setup.py" and our modules will be installed.


requirements.txt - is a file that lists all dependencies for a specific Python project. It can also specify specific versions.


setup.cfg - Contains options for the setup.py


MANIFEST.in - Specifies metadata for accompanying files


Pipfile - Dedicated file for project dependencies


What poetry does for me?

After implementing Poetry, you will have 2 new files.


pyproject.toml - List the details of packages added to the poetry tree

poetry.lock - Basic overview packages in the poetry tree


Creating a new poetry package

Creating our first poetry package is very simple.

$poetry new dolphin-poetry-project
$cd rp-poetry

A folder will be created "rp-poetry". Poetry will normalize packages for you automatically. The Poetry file structure is formatted as below.

dolphin-poetry-project
├── dolphin-poetry-project
│   └── __init__.py
├── tests/
│   ├── __init__.py
│   └── test_dolphin-poetry-project.py
│
├── README.md
└── pyproject.toml

pyproject.toml - This file will display our packages

[tool.poetry]
name = "dolphin-poetry-project"
version = "0.1.5"
description = "Dolphin test project"
authors = ["Dolphin <dolphin@example.com>"]

[tool.poetry.dependencies]
python = "^3.7"

[tool.poetry.dev-dependencies]
pytest = "^4.6"
invoke = "^1.8"
pycrypto = "^4.3"
strings = "^4.1"

[tool.strings]
remove-empty-directories = true

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

[tool.poetry.scripts]
run = "wsgi:main"

[tool.poetry.urls]
issues = "https://github.com/hackersandslackers/python-poetry-tutorial/issues"

poetry-project/poetry.lock

[[package]]name = "pytest"
version = "0.17.2"
description = "Python graph (network) package"
category = "dev"
optional = false
python-versions = "*"[[package]]
name = "attrs"
version = "20.3.0"
description = ""
category = "dev"
optional = false
python-versions = ">=2.7, !

[package.dependencies]
pytesting = ">=1.1.0"

[package.extras]

[[package]]name = "invoke"
version = "0.17.2"
description = "Python graph (network) package"
category = "dev"
optional = false
python-versions = "*"[[package]]
name = "attrs"
version = "20.3.0"
description = ""
category = "dev"
optional = false

[package.dependencies]

[package.extras]

The workflow

This can be allot to take in, so lets look at the workflow.

  1. Install Poetry

  2. Create a Poetry package

  3. Add a package

  4. Update the packages

  5. Track/list the packages

  6. Build the package

  7. Install the package

 

1. Installing on linux

  • A. curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

  • B. Check version with - poetry --version

  • C. Set environment variable - export PATH="$HOME/.poetry/bin:${PATH}"


2. Create a Poetry package

poetry new mypoetryproject


3. Adding packages

poetry add "package name"


4. Update the packages

Poetry update


5. Track/list the packages

poetry show --tree


6. Build the packages

Poetry build


7. Publishing Packages

poetry publish


8. Publishing Packages

poetry publish



USAGE

Poetry version 1.1.14

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.


101 views0 comments

Recent Posts

See All

Comments


bottom of page