ケース2
前回投稿した記事「Poetryでinstallコマンドを実行時に警告が出る場合の対処(Python)」にて、virtualenvs.prefer-active-pythonオプションをtrueに設定する方法でエラーを回避することができました。Poetryの設定ファイル(toml)とpyenv(global設定)によるPythonのカレントバージョンは以下のとおりであり、Poetryが要求するバージョンとPyenv(global)によるカレントバージョンが一致しているため警告なくPoetryによる仮想環境が構築できました。
Poetryの設定ファイル | Pyenv(global)の設定 | |
---|---|---|
Pythonバージョン | ^3.13 | 3.13.1 |
今回は、virtualenvs.prefer-active-pythonオプションをtrueに設定した環境下で、Pyenv(global)の設定とPoetryの設定ファイルで要求するバージョンが異なる場合の仮想環境構築を試します。
以下のケースで試すことにします。
Poetryの設定ファイル | Pyenv(global)の設定 | |
---|---|---|
Pythonバージョン | ^3.12 | 3.13.1 |
念のため Poetryのconfig設定を提示します。
$poetry config --list 3.12.8
cache-dir = "/Users/●●/Library/Caches/pypoetry"
experimental.system-git-client = false
installer.max-workers = null
installer.modern-installation = true
installer.no-binary = null
installer.parallel = true
keyring.enabled = true
solver.lazy-wheel = true
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.options.always-copy = false
virtualenvs.options.no-pip = false
virtualenvs.options.no-setuptools = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs" # /Users/●●/Library/Caches/pypoetry/virtualenvs
virtualenvs.prefer-active-python = true
virtualenvs.prompt = "{project_name}-py{python_version}"
warnings.export = true
単にPoetry install を実行しただけの場合(警告あり)
Poetryの設定ファイルが要求しているバージョンとPyenv(global)の設定バージョンが異なるため警告が表示されます。
$poetry install --no-root
The currently activated Python version 3.13.1 is not supported by the project (3.12.8).
Trying to find and use a compatible version.
Using python3.12 (3.12.8)
Creating virtualenv test in /Users/●●/Programing/pythonProject/test2/.venv
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
下線部分を意訳はおおむね以下のとおりかと。
「Pythonのカレントバージョンは3.13.1になっていますが、Poetryの設定ファイル(プロジェクト)では3.12.8が要求されています。バージョン3.12.8に互換性のあるバージョンを探します。」
3.12.8はインストール済みなので、該当バージョンを見つけ仮想環境の構築は成功しています。ただし、PATHの設定上pyenvはhomebrewのインストールディレクトリよりも優先した設定のはずなのになぜhomebrewのインストール先から3.12.8環境を構築しているのかは不明です。
$poetry env info
Virtualenv
Python: 3.12.8
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.12.8
Path: /opt/homebrew/opt/python@3.12/Frameworks/Python.framework/Versions/3.12
Executable: /opt/homebrew/opt/python@3.12/Frameworks/Python.framework/Versions/3.12/bin/python3.12
pyenv use実行後にpoetry installを実行する場合(警告なし)
poetry configでvirtualenvs.in-project = trueを設定しているので、仮想環境を構築するディレクトリ内で利用するPythonのバージョンを3.12.8に指定し、Poetryの設定ファイルで要求するPythonのバージョンとバージョンを合わせます。
$pyenv local 3.12.8
その後poetry installで仮想環境を構築します。警告なく環境構築が終了しました。
$poetry install --no-root 3.12.8
Creating virtualenv test in /Users/●●/Programing/pythonProject/test2/.venv
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
作成された環境を確認します。
念のため作成された仮想環境を確認します。今度はhomebrewではなくpyenvでインストールしたPythonを利用して仮想環境が作成されました。
$poetry env info
Virtualenv
Python: 3.12.8
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.12.8
Path: /Users/●●/.pyenv/versions/3.12.8
Executable: /Users/●●/.pyenv/versions/3.12.8/bin/python3.12
Poetry2.0以降の変更点
2025/01/06追記
仕様変更があり、デフォルトでvirtualenvs.use-poetry-python=trueと同じ動作になりました。
virtualenvs.use-poetry-python
Type:
boolean
Default:
false
Environment Variable:
POETRY_VIRTUALENVS_USE_POETRY_PYTHON
Introduced in 2.0.0
By default, Poetry will use the activated Python version to create a new virtual environment. If set to
Poetry Documentationtrue
, the Python version used during Poetry installation is used.
poetryのconfig からvirtualenvs.use-poetry-pythonを削除したいところですが、このconfig項目自体がver2.0から削除されているためか–unsetオプションでは削除できません。
そこで–migrateオプションを使用して不要なconfig項目を整理します。
~ ❯ poetry config virtualenvs.prefer-active-python --unset
Setting virtualenvs.prefer-active-python does not exist
~ ❯ poetry config --migrate
Checking for required migrations ...
virtualenvs.prefer-active-python = true -> virtualenvs.use-poetry-python = Not explicit set
Proceed with migration?: (yes/no) [no] yes
Config migration successfully done.