LoginSignup
3
2

More than 1 year has passed since last update.

poetryで誤ったバージョンの仮想環境が作られる問題について

Posted at

poetryで仮想環境を作成したはずの場所でpyproject.tomlで設定したpythonのバージョンが使われない問題があります。

pyproject.toml
[tool.poetry]
name = "test"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "3.8.2"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
shell
$ poetry -V
Poetry version 1.1.14

$ asdf local python 3.8.2 # pyenv local 3.8.2でも同じ
$ python -V
Python 3.8.2

$ poetry install
$ poetry run python -V
Python 3.10.5 #<- 3.8.2になっていない!

解決方法

poetryのissuesに解決方法が上がっていました。

① Poetryをversion 1.2にする

  • poetryをβ版(version 1.2.0)にアップデート
  • virtualenvs.prefer-active-python = trueにする
poetry self update --preview && \
poetry config virtualenvs.prefer-active-python true

注意点

poetry self updateをすると元のversion(1.1)に戻ってしまう

② .venvを手動で作成する

  • 対象のディレクトリ(pyproject.tomlのあるディレクトリ)に移動
  • 既に存在する場合、.venvとpoetry.lockを削除
    • poetry config virtualenvs.in-project trueの場合
    • そうでない場合、仮想環境はLinuxなら~/.cache/pypoetry/virtualenvs/、Macなら~/Library/Caches/pypoetry/virtualenvsにある
  • localで使用するpythonのversionを指定(pyenv, asdfなど)
  • 仮想環境(.venv)を作成
  • 仮想環境に入る
  • pipの更新
  • poetry install
rm -rf .venv
rm poetry.lock
pyenv local 3.8.2 #<-pyproject.tomlのpython versionに合わせる
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
poetry install

①の方がより根本的な解決かなと思います。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2