どうも、nippa です。
Python 記事は久しぶりになります。
今回は Python のパッケージの管理方法についてです。javascript や node.js、typescript だと npm、yarn などのパッケージ管理ツールがあります。
Python では requirements.txt を使って管理することが多い印象ですが、同じパッケージを2重でもったりするため非常に効率が悪くなりがちです。
そこで今回は、npm、yarn などの Python バージョンである Poetry についてまとめたいと思います。
環境
Poetry システム要件
プラットフォームは Windows/ macOS / Linux に対応しています。 現在は Python 2.7 もしくは Python3.5 以上であれば利用可能となっています。
ただし、公式としては今後システム要件を Python 3.6 以上でマルチプラットフォームで同等になるような対応を目指しているということなので、今後も使える形で記事をまとめたと思います。
Poetry のインストール
macOS / Linux の場合は以下の curl を使ってインストール可能です。
Python 3.6 以上のみ対応
curl -sSL curl -sSL https://install.python-poetry.org | python3 -
pip 等でもインストール可能ですが、公式は推奨していません。なので、推奨方法でのインストールのみ記載しています。
Path の設定
Poetry のインストール先は$HOME/.local/bin
なので、.zshrc / .bashrc に Path を通します。
# zshrc echo 'export PATH="$HOME/.local/bin:$PATH"' > ~/.zshrc # bashrc echo 'export PATH="$HOME/.local/bin:$PATH"' > ~/.bashrc
以前のバージョンだと$HOME/.poetry/bin:$PATH
にインストールされているので注意してください
サブコマンドの補完の設定
Poetry のサブコマンドのタブ補完をログインシェルで有効にしておくと、コマンドを検索する頻度が大幅に減るのでおすすめします。
タブ補完の設定は人により違いますので、環境に合わせて設定してください。
# Zsh (Homebrew) poetry completions zsh > $(brew --prefix)/share/zsh/site-functions # Zsh poetry completions zsh > ~/.zfunc/_poetry # Bash poetry completions bash > /etc/bash_completion.d/poetry.bash-completion # Bash (Homebrew) poetry completions bash > $(brew --prefix)/etc/bash_completion.d/poetry.bash-completion
これで Poetry の設定は完了です。
プロジェクトでの Poetry の設定
poetry-test
を作業ディレクトリとします。
mkdir poetry-test cd poetry-test
Poetry の初期設定
以下のコマンドで Poetry の初期設定を行います。
poetry init
プロジェクトの設定を聞かれます。必要に合わせて設定します。
This command will guide you through creating your pyproject.toml config. Package name [poetry-test]: Version [0.1.0]: Description []: Author [(Gitユーザー設定), n to skip]: License []: Compatible Python versions [^3.9]: Would you like to define your main dependencies interactively? (yes/no) [yes] You can specify a package in the following forms: - A single name (requests) - A name and a constraint (requests@^2.23.0) - A git url (git+https://github.com/python-poetry/poetry.git) - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop) - A file path (../my-package/my-package.whl) - A directory (../my-package/) - A url (https://example.com/packages/my-package-0.1.0.tar.gz) Search for package to add (or leave blank to continue): Would you like to define your development dependencies interactively? (yes/no) [yes] Search for package to add (or leave blank to continue): Generated file [tool.poetry] name = "poetry-test" version = "0.1.0" description = "" authors = ["(Gitユーザー設定)"] [tool.poetry.dependencies] python = "^3.9" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api" Do you confirm generation? (yes/no) [yes]
pyproject.toml
が生成され、このファイルに設定が記載されていますので、後ほど変更も可能です。
pyproject.toml
は以下のような内容になっています。
[tool.poetry] name = "poetry-test" version = "0.1.0" description = "" authors = ["author name"] [tool.poetry.dependencies] python = "^3.9" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"
Porter のパッケージ管理のコマンド
Poetry で使う必要最低限のコマンドをまとめておきます。
- 本番環境へのパッケージの追加
poetry add [パッケージ名]
- 開発環境へのパッケージの追加
poetry add -D [パッケージ名]
- パッケージコマンドの実行
poetry run [コマンド]
- パッケージの削除
poetry remove [パッケージ名]
- poetry.lock に記載のパッケージのインストール
poetry install
- package のアップデート
poetry update [パッケージ名]
インストールしたパッケージは.venv/lib/python[version]/site-packages
にインストールされます。
パッケージを追加するとpoetry.lock
が生成され、依存関係が記載されます。
感想
Python でパッケージ管理をするために、Poetry についてまとめました。
他にもパッケージ管理ツールはありますが、しばらく Poetry を使おうと思ってます。
現状の python の環境が複雑化してきており、以下のような状況になっています。
anyenv -> pyenv -> miniconda3(localhost) / poetry(project)
素で anaconda をインストールのは brew との衝突もあるので、したくないところです。
しばらくこの構成で様子をみたいと思います。
ではでは、また次回。