How to Set up Poetry

2020-01-12

Tags:
Categories:

Poetry = Python packaging and dependency management

Quick install

$ curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
$ source $HOME/.poetry/env
$ poetry completions bash | sudo dd of=/etc/bash_completion.d/poetry.bash-completion

Explained

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

This will add poetry to the PATH variable, in $HOME/.profile

But .profile is not sourced when opening a new terminal. We need to re-login to the GUI. For the moment:

$ source $HOME/.poetry/env

The following should configure bash completion:

$ poetry completions bash > /etc/bash_completion.d/poetry.bash-completion

It doesn't just work, because the current user does not have write acces to /etc. One possibility would be to run everything in a new shell with sudo:

$ sudo sh -c 'poetry completions bash > /etc/bash_completion.d/poetry.bash-completion'

This doesn't work either, because the new shell does not have the PATH configured yet. So we need to run poetry in the current shell and do only the redirection with sudo:

$ poetry completions bash | sudo dd of=/etc/bash_completion.d/poetry.bash-completion

Sources

Poetry README
https://github.com/python-poetry/poetry
When is .profile sourced
https://askubuntu.com/q/566399/189834
Sudo redirect
https://stackoverflow.com/q/82256/479288