😺
poetryのPythonと違ったバージョンのプロジェクトを作る
グローバルにインストールしたpoetryと違ったバージョンのPythonプロジェクトの環境をpoetry installすると、それっぽいメッセージは出るのですが、仮想環境のPythonのバージョンはpoetryのと同じになります。今回紹介する方法が良いのかどうか分かってないのですが、まず失敗してみてから、成功する例を紹介してみます。ここでのpoetryの設定はvirtualenvs.in-project = trueとします。
環境
- poetry 1.1.12 (Python 3.7.10)
 - pyenv 2.0.7
 
失敗例
$ mkdir tmp
$ cd tmp
$ pyenv local 3.8.12
$ poetry init
This command will guide you through creating your pyproject.toml config.
Package name [tmp]:  
Version [0.1.0]:  
Description []:  
Author [nnabeyang <nabeyang@example.com>, n to skip]:  
License []:  
Compatible Python versions [^3.7]:  ^3.8        
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
ここで作ったプロジェクトは成功例でもそのまま使います。この状態でpoetry installすると、ワーニングは出ますが3.8.12を見つけてくれたようなメッセージが出ます。
$ poetry install
The currently activated Python version 3.7.10 is not supported by the project (^3.8).
Trying to find and use a compatible version. 
Using python3 (3.8.12)
Creating virtualenv tmp in /Users/nabeyang/tmp/.venv
Updating dependencies
Resolving dependencies... (0.1s)
Writing lock file
Pythonのバージョンを調べてみましょう。
$ poetry shell
$ python -V
3.7.10
失敗してますね。では、仮想環境を消しておきましょう。
rm -rf .venv
成功例
pyenv local 3.8.12しているのでpython -Vすると3.8.12が返ってきます。この状態で先に仮想環境を作ってから、poetryを使います。
$ python -m venv .venv
$ poetry install
Pythonのバージョンを調べてみましょう。
$ poetry shell
$ python -V
3.8.12
成功してますね。
Discussion