Analytics Vidhya
Published in

Analytics Vidhya

Python packaging and dependency management using poetry

Introduction

Prerequisite

Installation

On Linux/macOS

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

On windows (PowerShell)

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python
Retrieving Poetry metadata# 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:$HOME/.poetry/binThis path will then be added to your `PATH` environment variable by
modifying the profile file located at:
$HOME/.profileYou can uninstall at any time by executing this script with the — uninstall option,
and these changes will be reverted.
Installing version: 1.0.5
— Downloading poetry-1.0.5-linux.tar.gz (23.00MB)
Poetry (1.0.5) is installed now. Great!To get started you need Poetry's bin directory ($HOME/.poetry/bin) in your `PATH`
environment variable. Next time you log in this will be done
automatically.
To configure your current shell run `source $HOME/.poetry/env`

Usage

Install dependencies and create virtual environment

[tool.poetry]name = "test-package"version = "0.1.0"description = "The description of the package goes here"license = "MIT"authors = ["MehrdadEP <mehrdadep@outlook.com>"]readme = 'README.md'  # Markdown files are supportedrepository = "https://github.com/python-poetry/poetry"homepage = "https://github.com/python-poetry/poetry"keywords = ['packaging', 'poetry']
[tool.poetry.dependencies]python = "~2.7 || ^3.2" # Compatible python versions must be declared here# Dependencies with extrasrequests = { version = "^2.13", extras = [ "security" ] }# Python specific dependencies with prereleases allowedpathlib2 = { version = "^2.2", python = "~2.7", allow-prereleases = true }# Git dependenciescleo = { git = "https://github.com/sdispater/cleo.git", branch = "master" }
# Optional dependencies (extras)pendulum = { version = "^1.4", optional = true }
[tool.poetry.dev-dependencies]pytest = "^3.0"pytest-cov = "^2.4"
poetry install

Other useful commands

  1. Add a new dependency to your existing environment using add command
poetry add pendulum
poetry build
poetry publish
poetry show --tree
poetry show --latest
poetry new test-project
poetry shell

Uninstalling poetry

python get-poetry.py --uninstall
POETRY_UNINSTALL=1 python get-poetry.py

--

--

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