Poetryとは
Pythonにおける依存性解決のためのパッケージマネージメントツールです。
Mac Linux Windowsいずれでも利用可能です。
Poetry install実行時に警告が出る
Poetryでpythonの実行環境を構築するために、以下のtest.tomlファイルを保存してあるとします。
(利用するPythonは3.13系列の最新バージョンを要求しています。)
[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = ["test <●@●.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.13"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
以下のコマンドを実行して、Poetryによる環境構築を行います。
$poetry install --no-root
すると、環境構築はできているようなのですが以下の警告が出ます。
The currently activated Python version 3.12.0 is not supported by the project (^3.13).
Trying to find and use a compatible version.
Using python3 (3.13.1)
Creating virtualenv test in /Users/●●/Programing/pythonProject/test2/.venv
下線部分を意訳はおおむね以下のとおりかと。
「Pythonのカレントバージョンは3.12.0になっていますが、Poetryの設定ファイル(プロジェクト)では3.13系列の最新版が要求されています。バージョン3.13に互換性のあるバージョンを探します。」
もちろん3.13.1はpyenvにより導入済みです。しかもpyenvによりpython3.13.1をグローバル設定しているため、python3.13.1がカレントバージョンではないというのはなぜかよく分かりません。
警告は出ましたが、作成した環境を確認すると以下の表示のようにVirtualenvはpython3.13.1になっています。
●●はユーザ名です。
$poetry env info
Virtualenv
Python: 3.13.1
Implementation: CPython
Path: /Users/●●/Programing/pythonProject/test2/.venv
Executable: /Users/●●/Programing/pythonProject/test2/.venv/bin/python
Valid: True
Base
Platform: darwin
OS: posix
Python: 3.13.1
Path: /Users/●●/.pyenv/versions/3.13.1
Executable: /Users/●●/.pyenv/versions/3.13.1/bin/python3.13
解決策1 poetry env useを使う
Poetryがinstallコマンドを実行する段階でPython3.13.1を認識していないようなので、明示的にどのバージョンを使うかをPoetryに教えます。
$poetry env use 3.13.1
Creating virtualenv test in /Users/●●/Programing/pythonProject/test2/.venv
Using virtualenv: /Users/●●/Programing/pythonProject/test2/.venv
この後poetry installを実行してpoetry仮想環境を作成します。
警告が出ずにコマンドが終了しました。
$poetry install --no-root
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
ただし、この方法はpoetry installする前に poetry env useコマンドを毎回実行する必要がありますが、pyenvでglobal指定しているバージョン以外を利用するときはこの方法で指定バージョンを利用することができます。
この点に関しては
を参照しました。
解決策2 virtualenvs.prefer-active-pythonオプションを使う
以下のコマンドを実行し、オプションを有効にします。
$poetry config virtualenvs.prefer-active-python true
この後poetry installを実行してpoetry仮想環境を作成します。
警告が出ずにコマンドが終了しました。
$poetry install --no-root
Creating virtualenv test in /Users/●●/Programing/pythonProject/test2/.venv
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
作成した環境を確認します。Virtualenvはpython3.13.1となっています。
$poetry env info
Virtualenv
Python: 3.13.1
Implementation: CPython
Path: /Users/●●/Programing/pythonProject/test2/.venv
Executable: /Users/●●/Programing/pythonProject/test2/.venv/bin/python
Valid: True
Base
Platform: darwin
OS: posix
Python: 3.13.1
Path: /Users/●●/.pyenv/versions/3.13.1
Executable: /Users/●●/.pyenv/versions/3.13.1/bin/python3.13
によると、virtualenvs.prefer-active-pythonオプションは、experimental(実験的)扱いとなっていますが、問題なく使えました。このオプションを有効にすると、PoetryはカレントバージョンのPythonを使おうとします。今回はPoetryの設定ファイルで要求するPythonのバージョンは3.13系列であり、pyenvによるglobal設定が3.13.1のため、両者のバージョンが一致しているため正常に仮想環境の作成が終了したようです。
ドキュメントには以下のように記載されています。
If you use a tool like pyenv to manage different Python versions, you can set the experimental
virtualenvs.prefer-active-python
option totrue
. Poetry will then try to find the currentpython
of your shell.For instance, if your project requires a newer Python than is available with your system, a standard workflow would be:
pyenv install 3.9.8 pyenv local 3.9.8 # Activate Python 3.9 for the current project poetry install