Python プロジェクトの作成 - Himeyama/himeyama GitHub Wiki

依存

Poetry のインストール

python -m pip install --upgrade pip
python -m pip install poetry

プロジェクトの作成

テンプレートの生成

poetry new PROJECT_NAME

pytest にバグがあるらしいので、書き換えておく。(https://stackoverflow.com/questions/69528842/an-error-while-trying-to-execute-tests-on-python-3-10-with-pytest)

E   TypeError: required field "lineno" missing from alias
=================================================================== short test summary info =================================================================== 
ERROR tests/test_xxxxx.py - TypeError: required field "lineno" missing from alias
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
====================================================================== 1 error in 0.12s ======================================================================= 
[tool.poetry.dev-dependencies]
- pytest = "^5.2"
+ pytest = "^7.0.1"

依存パッケージの追加

poetry add PIP_PACKAGE

flake8 のインストール flake8 が pyproject.toml に対応していないので、pyproject-flake8 をインストールする (将来的にできるようになるかも。)

poetry add -D pyproject-flake8

autopep8 のインストール フォーマッタ。

poetry add -D autopep8

パッケージのアップデート

poetry update

VSCode で開発

poetry config virtualenvs.in-project true

Ctrl + Shift + P で "Python: Select Interpreter" を選択し、Poetry のものを選ぶ。

実行

poetry run python xxxxx/xxxxx.py

テスト

tests/test_xxxxx.py にテストコードを記述し以下を実行。

poetry run pytest

Pip パッケージの作成

poetry build