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
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"
$ 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"
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
$ 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
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
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
$ 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
poetry show --latest