■ はじめに
https://dk521123.hatenablog.com/entry/2020/02/11/141852
で、以前、pipenv などを取り扱ったが、 新たに、Poetry(ポエトリー)なるものを教えてもらったのでメモ。
目次
【1】Poetry 1)公式サイト 【2】環境構築 1)前提条件 2)インストール(Linux / Windows) 3)Updateについて 【3】主な poetryコマンド 【4】Hello world 1)プロジェクト作成 2)仮想環境のセットアップ
【1】Poetry
* パッケージ依存関係のインストール・更新 * 仮想環境の構築 => JavaScript / TypeScript でいう「npm」「yarn」のような パッケージ管理ツールをイメージすれば。。。
Node.js ~ 基本編 / npm ~
https://dk521123.hatenablog.com/entry/2018/06/13/234315
1)公式サイト
https://python-poetry.org/
https://python-poetry.org/docs/
より抜粋 ~~~~~~~~~ Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. ~~~~~~~~~
【2】環境構築
1)前提条件
https://python-poetry.org/docs/#system-requirements
より抜粋 ~~~~~~~ Python 2.7 or 3.5+ ~~~~~~~
2)インストール(Linux / Windows)
https://python-poetry.org/docs/#osx--linux--bashonwindows-install-instructions
に記載されている。
Linux の場合
# Python3 がインストールされていない場合 sudo apt-get install python3 python3-distutils python --version # Poetryのインストール sudo curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python # または pip install poetry # 環境変数の設定(「poetry --version」で「poetry: command not found」だった場合) # echo 'export PATH=~/.poetry/bin:$PATH' >>~/.bash_profile # source ~/.bash_profile # 確認 poetry --version
Windows の場合
# PowerShell を「管理者権限(!重要:ここでハマった!)」で立ち上げ、以下を実行。 (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python - # 環境変数「Path」に「C:\Users\YourUserName\.poetry\bin」を追加 # 確認 poetry --version
3)Updateについて
# Poetryの更新
poetry self update
https://python-poetry.org/docs/#updating-poetry
【3】主な poetryコマンド
# | コマンド | 説明 |
---|---|---|
1 | poetry new | 新規プロジェクト作成 |
2 | poetry init | poetryプロジェクト初期化(pyproject.toml作成) |
3 | poetry install | 仮想環境のセットアップ |
4 | poetry add | パッケージの追加 |
5 | poetry update | 依存パッケージの更新 |
6 | poetry run python | Python実行 |
7 | poetry run pytest | PyTest実行 |
* 詳細についてだが、以下のサイトが分かりやすい。
https://qiita.com/ragnar1904/items/0e5b8382757ccad9a56c
【4】Hello world
1)プロジェクト作成
新規プロジェクト作成
# poetry new <Your-Package-Name> poetry new demo-poerty # こんな感じでできているはず demo-poerty ├── README.rst ... プロジェクト概要 (GithubのREADME.mdみたいなの) ├── demo_poerty ... pythonソースコード格納場所 │ └── __init__.py ├── pyproject.toml ... Poetryの依存関係などが記述されている管理ファイル └── tests ├── __init__.py └── test_demo_poerty.py
自分で作成 又は 既存システムのPoetry化
# 適当なフォルダ作成し、移動 mkdir demo-poerty cd demo-poerty # プロジェクト作成 poetry init -n # Q-Aで自分で設定したい場合 # poetry init # 動作確認 ls # 「pyproject.toml」ができているはず
pyproject.toml
[tool.poetry] name = "demo-poerty" version = "0.1.0" description = "" authors = ["Your Name <you@example.com>"] [tool.poetry.dependencies] python = "^3.8" [tool.poetry.dev-dependencies] [build-system] requires = ["poetry-core>=1.0.0"] build-backend = "poetry.core.masonry.api"
2)仮想環境のセットアップ
# poetry で設定したツール群をインストール
poetry install
参考文献
https://qiita.com/ksato9700/items/b893cf1db83605898d8a
https://www.sambaiz.net/article/288/
https://cocoatomo.github.io/poetry-ja/basic-usage/
https://www.lifewithpython.com/2020/01/python-tips-poetry-tasks.html
関連記事
パッケージ管理 Poetry ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2022/08/06/000000
仮想環境 ~ pyenv ~
https://dk521123.hatenablog.com/entry/2022/02/13/000000
仮想環境 ~ virtualenv / venv / pipenv ~
https://dk521123.hatenablog.com/entry/2020/02/11/141852
パッケージ管理 ~ pip ~
https://dk521123.hatenablog.com/entry/2021/07/02/000000
パッケージ管理 ~ conda ~
https://dk521123.hatenablog.com/entry/2022/02/11/000000
Node.js ~ 基本編 / npm ~
https://dk521123.hatenablog.com/entry/2018/06/13/234315
環境変数に関するあれこれ
https://dk521123.hatenablog.com/entry/2015/07/16/103501