1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Poetryのコマンドリストとpythonプログラムの配布方法

Posted at

はじめに

自作のPythonのプログラムを配布したあと、受け取った側の環境整備のサポートを極力しなくて良い方法を探していたときに poetry を見つけた。ややこしいルールなどを覚えなくとも使えたのでメモをしておく。

PoetryはPythonでの依存関係管理とパッケージングを行うツールです。プロジェクトが依存するライブラリを宣言し、それらを管理(インストール/更新)します。Poetryは、繰り返し可能なインストールを保証するためのロックファイルを提供し、プロジェクトを配布用にビルドすることもできる。

povetryでよく使うコマンドリスト

プロジェクトの初期設定と管理

コマンド 説明
poetry new [プロジェクト名] 新しいPoetryプロジェクトを作成します。
poetry init 既存のプロジェクトでPoetryを初期化します。

依存関係の管理

コマンド 説明
poetry add [パッケージ名] プロジェクトに新しい依存関係を追加します。
poetry install pyproject.tomlにリストされた依存関係をインストールします。
poetry update 依存関係を最新のバージョンに更新します。

仮想環境の操作

コマンド 説明
poetry shell 仮想環境を起動します。
poetry run [コマンド] 仮想環境内でコマンドを実行します。

その他

コマンド 説明
poetry -V Poetryのバージョンを確認します。

配布されたプログラムの実行

Githubでソースコードが配布されている場合

$ git clone https://github.com/<配布者>/<アプリ名>.git
$ cd <アプリ名>
$ poetry install
$ poetry run python <アプリ>

ソースコードを受け取った場合

受け取ったプログラム app.py を実行する場合
$ ls
app.py
pyproject.toml
$ poetry install
$ poetry run python app.py

pyproject.tomlはpoetryの管理用ファイル。直接手でイジるのはやめたほうが良い。

Poetryを使った環境構築

Poetry公式サイトから poetryのインストール方法

$ curl -sSL https://install.python-poetry.org | python3 -

Macでhomebrewでインストールする方法

$ brew install poetry
$ which poetry
/opt/homebrew/bin/poetry
$ poetry --version
Poetry (version 1.6.1)

ソースコードをPoetryで管理する

すでに開発が始まっているソースコードをPoetryで管理する場合について考える。

初期化

poetry init コメントを実行して、管理用ファイルpyproject.tomlを作成する。

$ cd <管理するソースコードの存在するディレクトリ>
$ poetry init
$ cat pyproject.toml
[tool.poetry]
name = "voice-browsing2"
version = "0.1.0"
description = "Control the web browser chrome by voice"
authors = ["Rai <rai@access-company.com>"]
license = "Apache License"
readme = "README.md"

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

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

新たにプロジェクトを作る場合

今回は利用しないが新たにプロジェクトを作る場合はpoetry newを実行する

$ poetry new <project-name>

必要なライブラリを確認し追加する

poetry run python app.py を実行しエラーがでた場合にはパッケージを poetry add <パッケージ名> で追加する。 addされたパッケージがpyproject.tomlファイルに追加され管理される。

$ poetry run python app.py
Creating virtualenv voice-browsing2-GjOHDj-9-py3.11 in /Users/Rai/Library/Caches/pypoetry/virtualenvs
Traceback (most recent call last):
  File "/Users/Rai/GitHub/voice_browsing2/app.py", line 1, in <module>
    from flask import Flask, render_template, request, jsonify
ModuleNotFoundError: No module named 'flask'
$ poetry add flask

仮想環境で実行

必要なライブラリをすべて追加し終わったあとアプリを実行する

$ poetry run python app.py

app.pyのフォルダ一式をGithubで公開したり、zipファイルなどで配布します。

Poetryの仮想環境の情報を確認

$ poetry env info
Virtualenv
Python:         3.11.6
Implementation: CPython
Path:           /Users/Rai/Library/Caches/pypoetry/virtualenvs/voice-browsing2-GjOHDj-9-py3.11
Executable:     /Users/Rai/Library/Caches/pypoetry/virtualenvs/voice-browsing2-GjOHDj-9-py3.11/bin/python
Valid:          True

System
Platform:   darwin
OS:         posix
Python:     3.11.6
Path:       /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11
Executable: /opt/homebrew/opt/python@3.11/Frameworks/Python.framework/Versions/3.11/bin/python3.11

whichコマンドでも仮想環境で利用されるpythonが確認できる

$ poetry run which python3
/Users/Rai/Library/Caches/pypoetry/virtualenvs/voice-browsing2-GjOHDj-9-py3.11/bin/python3

参考資料

1
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?