7rikazhexde’s tech log

技術的な興味関心、備忘録、アウトプットなどを書いています。

MacでPoetryをインストールする

Pythonではこれまでrequirements.txtでパッケージ管理していましたが、プロジェクト管理を1パッケージずつバージョンアップ管理するのが手間に感じたので、
Node.jsで使用されているnpmのpackage.json によるパッケージ管理と同様のことができるPoetryというプロジェクトを知り導入することにしました。

本記事ではMacにPoetryをインストールした際の手順や確認したことをまとめます。
また以下の記事ではGitHubリポジトリで管理している既存のプロジェクトに適用した例も紹介しています。

7rikazhexde-techlog.hatenablog.com

注意事項

本記事は20221029時点の情報です。
Poetryは都度アップデートされている状況のため、内容が古くなる可能性もありますのでご注意ください。
詳細は公式ドキュメントを確認してください。

Poetryのインストール

Poetryをサクッと使い始めてみるを参考にさせていただいたところ、
インストール時に非推奨と警告が表示されたため一度アンインストールし、公式の手順を確認しました。
本指定ではself updateコマンドで 1.2.0a1 以降にアップグレードすることができないようです。
※一部記載を環境変数に置き換えていますのでご注意ください。

% curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
Retrieving Poetry metadata

This installer is deprecated. Poetry versions installed using this script will not be able to use 'self update' command to upgrade to 1.2.0a1 or later. It is recommended to use https://install.python-poetry.org instead. Instructions are available at https://python-poetry.org/docs/#installation
# Welcome to Poetry!

This will download and install the latest version of Poetry,
a dependency and package manager for Python.

It will add the `poetry` command to Poetry's bin directory, located at:

$HOME/.poetry/bin

This path will then be added to your `PATH` environment variable by
modifying the profile files located at:

$HOME/.profile
$HOME/.zshrc
$HOME/.bash_profile

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing version: 1.1.14
  - Downloading poetry-1.1.14-darwin.tar.gz (70.76MB)

Poetry (1.1.14) is installed now. Great!

To get started you need Poetry's bin directory ($HOME/.poetry/bin) in your `PATH`
environment variable. Next time you log in this will be done
automatically.

To configure your current shell run `source $HOME/.poetry/env`

アンインストール

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python - --uninstall

公式のインストール手順

Installation -> With the official installerに従いインストールしました。
https://python-poetry.org/docs/master/#installing-with-the-official-installer
※一部記載を環境変数に置き換えていますのでご注意ください。

% curl -sSL https://install.python-poetry.org | python3 -
Retrieving Poetry metadata

# Welcome to Poetry!

This will download and install the latest version of Poetry,
a dependency and package manager for Python.

It will add the `poetry` command to Poetry's bin directory, located at:

$HOME/Library/Python/3.10/bin

You can uninstall at any time by executing this script with the --uninstall option,
and these changes will be reverted.

Installing Poetry (1.1.14): Done

Poetry (1.1.14) is installed now. Great!

To get started you need Poetry's bin directory ($HOME/Library/Python/3.10/bin) in your `PATH`
environment variable.

Add `export PATH="$HOME/Library/Python/3.10/bin:$PATH"` to your shell configuration file.

Alternatively, you can call Poetry explicitly with `$HOME/Library/Python/3.10/bin/poetry`.

You can test that everything is set up by executing:

`poetry --version`

poetryコマンドの実行パスをzprofileに環境変数$PATHを追加する

# poetry
export PATH="$HOME/Library/Python/3.10/bin:$PATH"

注意

poetry 1.6.1時点では以下のパスが表示されていました。
インストール時は出力内容を確認してください。

Add `export PATH="[$HOMEのパス]/.local/bin:$PATH"` to your shell configuration file.

コマンド実行

設定ファイル確認

% poetry config --list
cache-dir = "$HOME/Library/Caches/pypoetry"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.path = "{cache-dir}/virtualenvs"  # $HOME/Library/Caches/pypoetry/virtualenvs

仮想環境の設定変更

virtualenvs.in-projectの設定を変更して各プロジェクト配下に仮想環境を作るようにしました。
これで仮想環境の実体が入ったフォルダ(python.exe, 各パッケージ)を、 各プロジェクト直下の .venv に作成されるようになります。
(参考:#using-your-virtual-environment)

% poetry config virtualenvs.in-project true

% poetry config --list  
cache-dir = "$HOME/Library/Caches/pypoetry"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = true # 変更された
virtualenvs.path = "{cache-dir}/virtualenvs"  # $HOME/Library/Caches/pypoetry/virtualenvs

プロジェクトのセットアップ

プロジェクトのセットアップについては以下記事を参照ください。
非常にで分かりやすく紹介されていますので共有させていただきます。

仮想環境のセットアップ

上記記事を参考にさせていただき、poetry_testというプロジェクト名で仮想環境をセットアップしました。

% poetry new poetry_test
Created package poetry_test in poetry_test
% poetry install
Updating dependencies
Resolving dependencies... (9.8s)

Writing lock file

Package operations: 4 installs, 2 updates, 0 removals

  • Updating pyparsing (3.0.4 -> 3.0.9)
  • Updating attrs (21.4.0 -> 22.1.0)
  • Installing more-itertools (8.14.0)
  • Installing pluggy (0.13.1)
  • Installing py (1.11.0)
  • Installing pytest (5.4.3)

Installing the current project: poetry_test (0.1.0)

パッケージ追加

パッケージとして、requestsとPySimpleGUIを追加してみます。

% poetry add requests
Using version ^2.28.1 for requests

Updating dependencies
Resolving dependencies... (0.9s)

Writing lock file

Package operations: 4 installs, 1 update, 0 removals

  • Updating pyparsing (3.0.4 -> 3.0.9)
  • Installing charset-normalizer (2.1.1)
  • Installing idna (3.3)
  • Installing urllib3 (1.26.11)
  • Installing requests (2.28.1)

% poetry add pysimplegui
Using version ^4.60.3 for PySimpleGUI

Updating dependencies
Resolving dependencies... (0.3s)

Writing lock file

Package operations: 1 install, 1 update, 0 removals

  • Updating pyparsing (3.0.4 -> 3.0.9)
  • Installing pysimplegui (4.60.3)

% cat pyproject.toml 
[tool.poetry]
name = "poetry_test"
version = "0.1.0"
description = ""
authors = ["[user.name] <[user.mail]>"]

[tool.poetry.dependencies]
python = "^3.10"
requests = "^2.28.1" # 追加された
PySimpleGUI = "^4.60.3" # 追加された

[tool.poetry.dev-dependencies]
pytest = "^5.2"

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

<補足>パッケージ追加/削除

  • poetry addで追加すると(.venv) pip freezeで表示される
  • poetry removeで削除すると(.venv) pip freezeから表示されなくなる
  • (.venv)pip installでもパッケージをインストールできるが、pyproject.tomlには記録されない
[ユーザー名]@[コンピューター名] poetry_test % poetry remove pysimplegui
Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

Package operations: 0 installs, 0 updates, 1 removal

  • Removing pysimplegui (4.60.3)
[ユーザー名]@[コンピューター名] poetry_test % poetry shell             
Spawning shell within $HOME/develop/python/poetry_test/.venv
Restored session: 2022年 8月21日 日曜日 18時55分31秒 JST
[ユーザー名]@[コンピューター名] poetry_test % . $HOME/develop/python/poetry_test/.venv/bin/activate
(.venv) [ユーザー名]@[コンピューター名] poetry_test % pip freeze                                                        
(.venv) [ユーザー名]@[コンピューター名] poetry_test % pip install pysimplegui
Collecting pysimplegui
  Using cached PySimpleGUI-4.60.3-py3-none-any.whl (509 kB)
Installing collected packages: pysimplegui
Successfully installed pysimplegui-4.60.3
(.venv) [ユーザー名]@[コンピューター名] poetry_test % exit

Saving session...completed.
[ユーザー名]@[コンピューター名] poetry_test % cat pyproject.toml       
[tool.poetry]
name = "poetry_test"
version = "0.1.0"
description = ""
authors = ["[[user.name(git config)] <[user.email(git config)>"]

[tool.poetry.dependencies]
python = "^3.10"

[tool.poetry.dev-dependencies]

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

poetry addでパッケージ追加後、(.venv)でpipコマンドで削除してもpyproject.tomlには残る

[ユーザー名]@[コンピューター名] poetry_test % poetry add pysimplegui         
Using version ^4.60.3 for PySimpleGUI

Updating dependencies
Resolving dependencies... (0.1s)

Writing lock file

No dependencies to install or update
[ユーザー名]@[コンピューター名] poetry_test % poetry shell             
Spawning shell within $HOME/develop/python/poetry_test/.venv
Restored session: 2022年 8月21日 日曜日 18時56分37秒 JST
[ユーザー名]@[コンピューター名] poetry_test % . $HOME/develop/python/poetry_test/.venv/bin/activate
(.venv) [ユーザー名]@[コンピューター名] poetry_test % pip freeze
PySimpleGUI==4.60.3 #存在する
(.venv) [ユーザー名]@[コンピューター名] poetry_test % pip uninstall pysimplegui
Found existing installation: PySimpleGUI 4.60.3
Uninstalling PySimpleGUI-4.60.3:
  Would remove:
    $HOME/develop/python/poetry_test/.venv/bin/psghelp
    $HOME/develop/python/poetry_test/.venv/bin/psgissue
    $HOME/develop/python/poetry_test/.venv/bin/psgmain
    $HOME/develop/python/poetry_test/.venv/bin/psgsettings
    $HOME/develop/python/poetry_test/.venv/bin/psgupgrade
    $HOME/develop/python/poetry_test/.venv/bin/psgver
    $HOME/develop/python/poetry_test/.venv/lib/python3.10/site-packages/PySimpleGUI-4.60.3.dist-info/*
    $HOME/develop/python/poetry_test/.venv/lib/python3.10/site-packages/PySimpleGUI/*
Proceed (Y/n)? Y
  Successfully uninstalled PySimpleGUI-4.60.3 # pipコマンドでuninstallした
(.venv) [ユーザー名]@[コンピューター名] poetry_test % exit

Saving session...completed.

[ユーザー名]@[コンピューター名] poetry_test % cat pyproject.toml       
[tool.poetry]
name = "poetry_test"
version = "0.1.0"
description = ""
authors = ["[[user.name(git config)] <[user.email(git config)>"]

[tool.poetry.dependencies]
python = "^3.10"
PySimpleGUI = "^4.60.3" #消えていない

[tool.poetry.dev-dependencies]

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

Poetryのバージョンアップデート

Poetryのバージョンアップはpoetry self updateを実行します。

% poetry self update
Updating Poetry to 1.2.2

Updating dependencies
Resolving dependencies... (15.2s)

Package operations: 10 installs, 12 updates, 0 removals

  - Updating certifi (2022.6.15 -> 2022.9.24)
  - Updating idna (3.3 -> 3.4)
  - Installing more-itertools (9.0.0)
  - Installing pycparser (2.21)
  - Updating urllib3 (1.26.11 -> 1.26.12)
  - Installing attrs (22.1.0)
  - Installing cffi (1.15.1)
  - Updating distlib (0.3.5 -> 0.3.6)
  - Installing jaraco.classes (3.2.3)
  - Updating poetry-core (1.0.8 -> 1.3.2)
  - Installing pyrsistent (0.18.1)
  - Updating cachecontrol (0.12.11 -> 0.12.12)
  - Updating cleo (0.8.1 -> 1.0.0a5)
  - Installing dulwich (0.20.46)
  - Installing jsonschema (4.16.0)
  - Updating keyring (23.8.2 -> 23.9.3)
  - Updating packaging (20.9 -> 21.3)
  - Installing poetry-plugin-export (1.1.2)
  - Updating tomlkit (0.11.4 -> 0.11.6)
  - Updating virtualenv (20.16.3 -> 20.16.6)
  - Installing xattr (0.9.9)
  - Updating poetry (1.1.14 -> 1.2.2)

Updating the poetry script

Poetry (1.2.2) is installed now. Great!

以上です。