Skip to content

Instantly share code, notes, and snippets.

@karma-git
Created January 1, 2022 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karma-git/2690afc72cb10197440fb7f3c5384d74 to your computer and use it in GitHub Desktop.
Save karma-git/2690afc72cb10197440fb7f3c5384d74 to your computer and use it in GitHub Desktop.
Poetry CheatSheet

Overview

Poetry is a modern python package manager like npm.

Poetry — продвинутая замена pip для Python. Менеджер зависимостей, который нам нужен в 2021м! - by Диджитализируй!

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
poetry --version
# Oh-My-Zsh
mkdir $ZSH_CUSTOM/plugins/poetry
poetry completions zsh > $ZSH_CUSTOM/plugins/poetry/_poetry

Init inside an existed directory

mkdir demo-poetry-pj && cd demo-poetry-pj
poetry init

You'll see This command will guide you through creating your pyproject.toml config. and you'll get pyproject.toml as the result.

pyproject.toml content
[tool.poetry]
name = "demo-poetry-pj"
version = "0.1.0"
description = "My Demo package!"
authors = ["Andrei Horbach <andrewhorbach@gmail.com>"]
license = "MIT"

[tool.poetry.dependencies]
python = "^3.10"
fastapi = "^0.70.1"

[tool.poetry.dev-dependencies]

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

Init New project

$ poetry new demo-po-new
Created package demo_po_new in demo-po-new

Let's look inside:

$ tree demo-po-new 
demo-po-new
├── README.rst
├── demo_po_new
│   └── __init__.py
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_demo_po_new.py

2 directories, 5 files
pyproject.toml content
[tool.poetry]
name = "demo-po-new"
version = "0.1.0"
description = ""
authors = ["Andrei Horbach <andrewhorbach@gmail.com>"]

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

[tool.poetry.dev-dependencies]
pytest = "^5.2"

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

Poetry virtual environment

Create

$ poetry env use python3.10
Creating virtualenv demo-po-new-I_6fpj5G-py3.10 in ~/Library/Caches/pypoetry/virtualenvs
Using virtualenv: ~/Library/Caches/pypoetry/virtualenvs/demo-po-new-I_6fpj5G-py3.10

Activate

$ poetry shell          
Spawning shell within ~/Library/Caches/pypoetry/virtualenvs/demo-po-new-I_6fpj5G-py3.10

Deactivate

exit

One shot task

That is mean that you need just to run one specific command once with your packages, but you won't activate venv for it.

something like:

poetry run python main.py

Manipulating with packages

Add package

$ poetry add requests
output
❯ poetry add requests      
Using version ^2.26.0 for requests

Updating dependencies
Resolving dependencies... (6.8s)

Writing lock file

Package operations: 13 installs, 0 updates, 0 removals

  • Installing pyparsing (3.0.6)
  • Installing attrs (21.4.0)
  • Installing certifi (2021.10.8)
  • Installing charset-normalizer (2.0.9)
  • Installing idna (3.3)
  • Installing more-itertools (8.12.0)
  • Installing packaging (21.3)
  • Installing pluggy (0.13.1)
  • Installing py (1.11.0)
  • Installing urllib3 (1.26.7)
  • Installing wcwidth (0.2.5)
  • Installing pytest (5.4.3)
  • Installing requests (2.26.0)

NOTE: this will add poetry.lock

Show dependencies tree

poetry show --tree
output
❯ poetry show --tree
pytest 5.4.3 pytest: simple powerful testing with Python
├── atomicwrites >=1.0
├── attrs >=17.4.0
├── colorama *
├── more-itertools >=4.0.0
├── packaging *
│   └── pyparsing >=2.0.2,<3.0.5 || >3.0.5 
├── pluggy >=0.12,<1.0
├── py >=1.5.0
└── wcwidth *
requests 2.26.0 Python HTTP for Humans.
├── certifi >=2017.4.17
├── charset-normalizer >=2.0.0,<2.1.0
├── idna >=2.5,<4
└── urllib3 >=1.21.1,<1.27

Run commands

Add fallowing section into pyproject.toml:

...
[tool.poetry.scripts]
hi = "demo_po_new.hello_world:greetings"

Create python module hello_world.py:

❯ tree demo_po_new -I __pycache__
demo_po_new
├── __init__.py
└── hello_world.py

0 directories, 2 files

Create function iside the module:

def greetings() -> None:
    print("Hello world")

Run the function via poetry:

$ poetry run hi                  
Hello world

Ok, where is my venv located?

$ poetry env info       
output
Virtualenv
Python:         3.10.0
Implementation: CPython
Path:           ~/Library/Caches/pypoetry/virtualenvs/demo-po-new-I_6fpj5G-py3.10
Valid:          True

System
Platform: darwin
OS:       posix
Python:   /Library/Frameworks/Python.framework/Versions/3.10

Other

  • poetry show --latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment