« Back to Index

[Python Poetry]

View original Gist on GitHub

Tags: #python3 #poetry

Python Poetry.bash

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

# reload .bash_profile and check poetry version
poetry --version

# update poetry to latest version
poetry self:update

# generate auto-complete for Homebrew installed version of bash
poetry completions bash > $(brew --prefix)/etc/bash_completion.d/poetry.bash-completion

# install python version
pyenv install 2.7.15

# check help for poetry init (which generates a `pyproject.toml`)
# poetry doesn't allow installing packages via cli (they need to be specified in toml)
poetry init -h

# create pyproject.toml interactively (see below for generated `pyproject.toml`)
# 
# notice [tool.poetry.dependencies] specifies the Python version used (this is required!).
poetry init

# install dependencies
poetry install

# add additional dependencies (use --dev for dev dependency)
poetry add requests <...>
poetry add --dev requests <...>

# execute commands within the virtual environment (e.g. dev dependency ipython was installed)
poetry run ipython

# load virtual environment permanently for the current shell (e.g. now python version will be the expected environment, not the OS version)
poetry shell
python --version

# here is a shortened Python3 example, as the above uses the OS default of Python2 for installing `2.7.15`
# where as if you tried to set the Python version in the `pyproject.toml` to `^3.7` it would fail as that Python version wouldn't be available
# it means whenever you want to setup a new Python3 environment, you'll need a compatible Python interpreter running first.
# e.g. if you want to install 3.7.1 you'll need 3.7.3 running first to execute Poetry (this isn't necessary with Python2 as we already had 2.7 available by the OS)
pyenv install 3.7.3
pyenv local 3.7.3
poetry add boto3 pytest structlog tornado
poetry add --dev flake8 flake8-import-order mypy tox ipython

pyproject.toml

[tool.poetry]
name = "2.7.15"
version = "0.1.0"
description = ""
authors = ["Integralist <mark.mcdx@gmail.com>"]

[tool.poetry.dependencies]
python = "^2.7"
boto3 = "^1.9"
pytest = "^4.4"
structlog = "^19.1"
tornado = "^4.0" # specified explicitly (others were latest versions) as latest didn't support Python 2.7

[tool.poetry.dev-dependencies]
flake8 = "^3.7"
flake8-import-order = "^0.18.1"
ipython = "^5.0"

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