2
0

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 5 years have passed since last update.

pythonのpackage依存関係と仮想環境管理ツールPoetry

2
Posted at

Poetryは何?

  • python package依存関係を管理する
  • プロジェクト専用の仮想環境を構築できる(
    • 依存するpython versionを指定できる
    • 依存するpackage versionを指定できる
  • ruby bundleと似てるツール

インストール

https://python-poetry.org/docs/
macの場合のpython scriptでインストール

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

pipを使ってインストール

pip install --user poetry

設定しておくと便利なこと

以下のように設定すると、仮想環境がproject folderの以下で作成され、vscodeがpython環境を自動識別できるので、vscode intellisenseが機能する

poetry config virtualenvs.in-project true

projectにpoetryを導入

以下のコマンドでpyproject.toml(rubyのgemfileみたいなもの)を作成する

poetry init

依存するpackageを追加

以下のコマンドで依存するpackageを仮想環境へinstallしつづ、pyproject.tomlに依存packageが指定される

poetry add pendulum
poetry add pendulum@^2.0.5  # versionも指定する場合
poetry add "pendulum>=2.0.5"  # versionも指定する場合

pyproject.toml, poetry.lockに設定した依存packageをinstall

poetry install

packageをアップデートする

依存する全てのpackageをアップデートしたい場合

poetry update

特定のpackageをアップデートしたい場合

poetry update fastapi

仮想環境でコードを実行したい時

仮想環境に入りたい場合

poetry shell 

仮想環境で何かを実行したい場合

poetry run uvicorn main:app --reload

package依存関係を表示したい場合

poetry show --tree

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?