🐍
Poetryを1.8.x -> 2系に変更する
なぜ記事にしたか
2系にすることで、コマンドなどいろいろ変更点があるようなのでまとめていって、最終的に新規プロジェクトを作成し、開発できるようになることを確認する。
インストール(asdf)
$ asdf plugin-update poetry
$ asdf install poetry 2.1.1
$ asdf global poetry 2.1.1
$ asdf reshim poetry
$ poetry --version
Poetry (version 2.1.1)
正確な機能のキャッチアップはこちらから
上記の公式サイトをすべて見ることが健全である。が、自分の中で差分として理解すべきことを記載していきます。
pyproject.toml
[project]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = [
{name = "Sébastien Eustace", email = "sebastien@eustace.io"}
]
readme = "README.md"
requires-python = ">=3.8"
[tool.poetry]
packages = [{include = "poetry_demo"}]
[build-system]
requires = ["poetry-core>=2.0"]
build-backend = "poetry.core.masonry.api"
1.8.xからバージョンアップする場合に重要なコマンド
poetry check
コマンドを実行してみましょう。私はこうなりました。
$ poetry check
Warning: [tool.poetry.name] is deprecated. Use [project.name] instead.
Warning: [tool.poetry.version] is set but 'version' is not in [project.dynamic]. If it is static use [project.version]. If it is dynamic, add 'version' to [project.dynamic].
If you want to set the version dynamically via `poetry build --local-version` or you are using a plugin, which sets the version dynamically, you should define the version in [tool.poetry] and add 'version' to [project.dynamic].
Warning: [tool.poetry.description] is deprecated. Use [project.description] instead.
Warning: [tool.poetry.readme] is set but 'readme' is not in [project.dynamic]. If it is static use [project.readme]. If it is dynamic, add 'readme' to [project.dynamic].
If you want to define multiple readmes, you should define them in [tool.poetry] and add 'readme' to [project.dynamic].
Warning: [tool.poetry.authors] is deprecated. Use [project.authors] instead.
修正が成功すると、以下のようになります。
$ poetry check
All set!
[project]セクション
以前だと[tool.poetry]
セクションで記載されていたものが[project]
となりました。
[tool.poetry]でパッケージモードを明確にする必要がある
自身をパッケージ化するかどうか、これは大きな書き方の違いです。パッケージ開発ではない場合は以下のように記載する必要があります。
[tool.poetry]
package-mode = false
コマンド
poetry shellがなくなった
$ poetry env activate
こちらだけを使います。vscodeのコードインタープリターを読み込ませるために頻繁にこのコマンドをつかっていたため、衝撃でした。
poetryを新規で始める手順
poetry init
$ poetry init
initでどんどんEnterを押して作成してきます。メアドだけNoにした結果、以下のファイルが作成されました。
project.toml
[project]
name = "openai-computeruse"
version = "0.1.0"
description = ""
authors = [
{name = "Your Name",email = "you@example.com"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
プロジェクトローカルで仮想化
$ poetry config virtualenvs.in-project true --local
poetry.toml
が作成されます。これで、プロジェクト内でパッケージ管理できます。
poetry.toml
[virtualenvs]
in-project = true
pyproject.tomlの自身のパッケージ化falseを追加設定
手動でファイルを編集しました。
project.toml
[project]
name = "openai-computeruse"
version = "0.1.0"
description = ""
authors = [
{name = "Your Name",email = "you@example.com"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
]
[tool.poetry] <----追加
package-mode = false <----追加
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
poetry installしてみる
仮想化できるかどうかパッケージのADDはせずに実行してみます。問題なくインストールができたようです。
$ poetry install
Creating virtualenv openai-computeruse in /Users/xxx/openai-computeruse/.venv
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
poetry add を試してみる
add したあとのpyproject.toml
を見てみたかったので試してみました。
$ poetry add openai
project.toml
[project]
name = "openai-computeruse"
version = "0.1.0"
description = ""
authors = [
{name = "Your Name",email = "you@example.com"}
]
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"openai (>=1.66.3,<2.0.0)" <----追加
]
[tool.poetry]
package-mode = false
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"
[project]
内のdependencies
に追記がされることがわかりました。
vscodeでインタープリター設定確認
$ poetry env activate
問題なくインタープリターが.venv
担っていました。python開発におけるハマりどころもクリアされていますね。
無事に2.xに入門できました。
メジャーなのでバージョンアップするのはかなり渋ると思いますが、逃げられない...という悩みから1つ開放されて幸せです。
Discussion