Pyenv + PoetryでPythonの環境を作る。(覚書)

## 目的
個人的なPythonの開発環境を作るために、PyenvとPoetryでパッケージ管理とPythonのバージョン管理を実施する。

## Pyenv+Poetryの設定

```
% pyenv versions
  system
  3.8.13
* 3.9.11 (set by /path/to/project/.python-version)
% pyenv local 3.8.13
% poetry env use 3.8

  NoCompatiblePythonVersionFound

  The specified Python version (3.8.13) is not supported by the project (3.9.11).
  Please choose a compatible version or loosen the python constraint specified in the pyproject.toml file.

  at /usr/local/Cellar/poetry/1.1.13/libexec/lib/python3.10/site-packages/poetry/utils/env.py:708 in create_venv
       704│             # If an executable has been specified, we stop there
       705│             # and notify the user of the incompatibility.
       706│             # Otherwise, we try to find a compatible Python version.
       707│             if executable:
    →  708│                 raise NoCompatiblePythonVersionFound(
       709│                     self._poetry.package.python_versions, python_patch
       710│                 )
       711│ 
       712│             io.write_line(
% poetry env use Python3.8

  NoCompatiblePythonVersionFound

  The specified Python version (3.8.13) is not supported by the project (3.9.11).
  Please choose a compatible version or loosen the python constraint specified in the pyproject.toml file.

  at /usr/local/Cellar/poetry/1.1.13/libexec/lib/python3.10/site-packages/poetry/utils/env.py:708 in create_venv
       704│             # If an executable has been specified, we stop there
       705│             # and notify the user of the incompatibility.
       706│             # Otherwise, we try to find a compatible Python version.
       707│             if executable:
    →  708│                 raise NoCompatiblePythonVersionFound(
       709│                     self._poetry.package.python_versions, python_patch
       710│                 )
       711│ 
       712│             io.write_line(

```

NoCompatiblePythonVersionFoundというエラーが出たので、pyproject.tomlの方のpythonのバージョンを修正する。

```pyproject.toml
[tool.poetry]
name = "PythonCronLambda"
version = "0.1.0"
description = ""
authors = ["ShiroUz"]

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

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

```

再度、poetryで使用する、pythonのバージョンを設定する。
```
% poetry env use Python3.8
Recreating virtualenv pythoncronlambda in /path/to/project/.venv
Using virtualenv: /path/to/project/.venv
```

poetry addしてみる。
```
poetry add requests
Using version ^2.27.1 for requests

Updating dependencies
Resolving dependencies... (2.2s)

Writing lock file

Package operations: 5 installs, 0 updates, 0 removals

  • Installing certifi (2021.10.8)
  • Installing charset-normalizer (2.0.12)
  • Installing idna (3.3)
  • Installing urllib3 (1.26.9)
  • Installing requests (2.27.1)

```

poetry.lockを確認する。

```poetry.lock
[[package]]
name = "certifi"
version = "2021.10.8"
description = "Python package for providing Mozilla's CA Bundle."
category = "main"
optional = false
python-versions = "*"

[[package]]
name = "charset-normalizer"
version = "2.0.12"
description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet."
category = "main"
optional = false
python-versions = ">=3.5.0"

[package.extras]
unicode_backport = ["unicodedata2"]

[[package]]
name = "idna"
version = "3.3"
description = "Internationalized Domain Names in Applications (IDNA)"
category = "main"
optional = false
python-versions = ">=3.5"

[[package]]
name = "requests"
version = "2.27.1"
description = "Python HTTP for Humans."
category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*"

[package.dependencies]
certifi = ">=2017.4.17"
charset-normalizer = {version = ">=2.0.0,<2.1.0", markers = "python_version >= \"3\""}
idna = {version = ">=2.5,<4", markers = "python_version >= \"3\""}
urllib3 = ">=1.21.1,<1.27"

[package.extras]
socks = ["PySocks (>=1.5.6,!=1.5.7)", "win-inet-pton"]
use_chardet_on_py3 = ["chardet (>=3.0.2,<5)"]

[[package]]
name = "urllib3"
version = "1.26.9"
description = "HTTP library with thread-safe connection pooling, file post, and more."
category = "main"
optional = false
python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, <4"

[package.extras]
brotli = ["brotlicffi (>=0.8.0)", "brotli (>=1.0.9)", "brotlipy (>=0.6.0)"]
secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "ipaddress"]
socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"]

[metadata]
lock-version = "1.1"
python-versions = "3.8.13"
content-hash = "063bcb19cc6b71b9f048537298564efb5061b307dc6e069a0807c7dfea65c42c"

[metadata.files]
certifi = [
    {file = "certifi-2021.10.8-py2.py3-none-any.whl", hash = "sha256:d62a0163eb4c2344ac042ab2bdf75399a71a2d8c7d47eac2e2ee91b9d6339569"},
    {file = "certifi-2021.10.8.tar.gz", hash = "sha256:78884e7c1d4b00ce3cea67b44566851c4343c120abd683433ce934a68ea58872"},
]
charset-normalizer = [
    {file = "charset-normalizer-2.0.12.tar.gz", hash = "sha256:2857e29ff0d34db842cd7ca3230549d1a697f96ee6d3fb071cfa6c7393832597"},
    {file = "charset_normalizer-2.0.12-py3-none-any.whl", hash = "sha256:6881edbebdb17b39b4eaaa821b438bf6eddffb4468cf344f09f89def34a8b1df"},
]
idna = [
    {file = "idna-3.3-py3-none-any.whl", hash = "sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff"},
    {file = "idna-3.3.tar.gz", hash = "sha256:9d643ff0a55b762d5cdb124b8eaa99c66322e2157b69160bc32796e824360e6d"},
]
requests = [
    {file = "requests-2.27.1-py2.py3-none-any.whl", hash = "sha256:f22fa1e554c9ddfd16e6e41ac79759e17be9e492b3587efa038054674760e72d"},
    {file = "requests-2.27.1.tar.gz", hash = "sha256:68d7c56fd5a8999887728ef304a6d12edc7be74f1cfa47714fc8b414525c9a61"},
]
urllib3 = [
    {file = "urllib3-1.26.9-py2.py3-none-any.whl", hash = "sha256:44ece4d53fb1706f667c9bd1c648f5469a2ec925fcf3a776667042d645472c14"},
    {file = "urllib3-1.26.9.tar.gz", hash = "sha256:aabaf16477806a5e1dd19aa41f8c2b7950dd3c746362d7e3223dbe6de6ac448e"},
]

```
poetry.lockファイルができていた。
これをGitで他の人に共有し、pyenvで指定のバージョンでpythonを使える状態で、`poetry install`コマンドを実行すれば同じ環境になるはず。

## 感想
requirements.txtよりもこっちの方法の方が同じ環境で開発できそう。