【Python】poetryを使った環境構築

python-logo Python

Poetryとは

Python用のパッケージ管理ツール。

依存パッケージの追加、削除、インストール、仮想環境でのPython実行などができる。

似たようなものはpipやpipenv、pyflowなどが存在する。

poetryの選定にはそこまでこだわりはないが、npmなどとコマンド体系が似ているので採用した。

環境

  • Pythonのバージョンは3.10.0

公式ドキュメント

Poetry documentation (ver. 1.1.6 日本語訳)
Poetry documentation translated into Japanese

インストール

公式ドキュメントではcurlコマンドによるインストールを推奨しているが、
homebrewを使ってもインストールすることはできるので今回はこちらを使う。

brew install poetry

インストールすることができた。

$ poetry -V
Poetry (version 1.3.2)

プロジェクト初期化

poetry init

プロジェクト名やインストールするパッケージなどを対話的に質問され、回答していくとプロジェクトが作成される。

プロジェクトが作成されるとpyproject.tomlというファイルが作成される。

プログラム実行

コマンド - Poetry documentation (ver. 1.1.6 日本語訳)
Poetry documentation translated into Japanese

main.pyというファイルを作成し、helloworldを出力するように記述する。

def main():
    print('hello, world.')

if __name__ == "__main__":
    main()

poetryの仮想環境上でこのファイルを実行するには、以下のコマンドを実行する。

フォーマットは以下の通り。

poetry run 仮想環境で実行したいコマンド

# 「python main.py」コマンドを仮想環境で実行する
$ poetry run python main.py
hello, world.

# 仮想環境で利用されているpythonのバージョンを確認する
# PCには3.10.0のバージョンが利用されているが、仮想環境では3.11.2のバージョンが使われている
$ poetry run python --version
Python 3.11.2
$ python --version
Python 3.10.0

「hello, world」が出力され、main.pyが実行されることが確認できた。

ちなみにpoetryには「npm run dev」のようなタスクランナー機能はない。

ヘルプ

poetryコマンドを単体で実行すればヘルプを見ることができ、
これを見ればパッケージインストール時は「poetry add パッケージ名」などが分かる。

$ poetry
Poetry (version 1.3.2)

Usage:
  command [options] [arguments]

Options:
  -h, --help                 Display help for the given command. When no command is given display help for the list command.
  -q, --quiet                Do not output any message.
  -V, --version              Display this application version.
      --ansi                 Force ANSI output.
      --no-ansi              Disable ANSI output.
  -n, --no-interaction       Do not ask any interactive question.
      --no-plugins           Disables plugins.
      --no-cache             Disables Poetry source caches.
  -C, --directory=DIRECTORY  The working directory for the Poetry command (defaults to the current working directory).
  -v|vv|vvv, --verbose       Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.

Available commands:
  about              Shows information about Poetry.

# 略

まとめ

  • poetry initでプロジェクト初期化
  • 依存関係などの設定値は全てpyproject.tomlに書かれている
  • 各種コマンドはpoetryコマンドで分かる

トラブルシューティング

brewのインストールで以下のエラーが発生した。

Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/2to3
Target /usr/local/bin/2to3
already exists. You may want to remove it:
  rm '/usr/local/bin/2to3'

To force the link and overwrite all conflicting files:
  brew link --overwrite python@3.11

To list all files that would be deleted:
  brew link --overwrite --dry-run python@3.11

Possible conflicting files are:
/usr/local/bin/2to3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/2to3
/usr/local/bin/idle3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/idle3
/usr/local/bin/pydoc3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/pydoc3
/usr/local/bin/python3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
/usr/local/bin/python3-config -> /Library/Frameworks/Python.framework/Versions/3.7/bin/python3-config
Error: Permission denied @ dir_s_mkdir - /usr/local/Framework

とりあえずエラーログに従ってコマンド実行。

rm /usr/local/bin/2to3

またエラーログに従ってドライラン

$ brew link --overwrite --dry-run python@3.11
brew link --overwrite --dry-run python@3.11
Would remove:
/usr/local/bin/idle3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/idle3
/usr/local/bin/pydoc3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/pydoc3
/usr/local/bin/python3 -> /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
/usr/local/bin/python3-config -> /Library/Frameworks/Python.framework/Versions/3.7/bin/python3-config
Error: Permission denied @ dir_s_mkdir - /usr/local/Frameworks

/usr/local/Frameworksのパーミッションで怒られている。

brew doctorで確認してみる。

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: Some installed kegs have no formulae!
This means they were either deleted or installed manually.
You should find replacements for the following formulae:
  protobuf@3.7

Warning: The following directories do not exist:
/usr/local/Frameworks
/usr/local/sbin

You should create these directories and change their ownership to your user.
  sudo mkdir -p /usr/local/Frameworks /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/Frameworks /usr/local/sbin

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/libDellMonitorSdkLib.dylib
// 略

指摘の通り対象のディレクトリが存在しなかったので、作成する。

$ ls /usr/local/Frameworks
ls: /usr/local/Frameworks: No such file or directory
$ ls /usr/local/sbin
ls: /usr/local/sbin: No such file or directory

# エラーログ通りに作成
$ sudo mkdir -p /usr/local/Frameworks /usr/local/sbin

$ ls /usr/local/sbin /usr/local/Frameworks
/usr/local/Frameworks:

/usr/local/sbin:

# パーミッションも修正
$ sudo chown -R $(whoami) /usr/local/Frameworks /usr/local/sbin

再度インストールを試みる。

$ brew install poetry

# 無事インストールできた
$ poetry -V
Poetry (version 1.3.2)
タイトルとURLをコピーしました