LoginSignup
18
20

More than 3 years have passed since last update.

pyenv+pipx+PoetryでPythonのきれいな開発環境を手に入れる

Last updated at Posted at 2019-12-16

以前話題になっていたこちらの記事の通りです。…というのも無価値な記事になってしまうので、多少解説をします。

My Python Development Environment, 2020 Edition

こちらの記事の著者はWSLを使っていますが、私もOSX環境でもほぼこのまま使えています。

pyenv

Why? I need to run multiple Python versions, isolated from the system Python. pyenv makes it easy to install, manage, and switch between those multiple Pythons.

様々なPythonのバージョンを切り替えるツールです。ちなみにもとの記事にある通り、pyenvは開発環境用に使い、アプリケーション実行環境にはDockerを利用すべきです。

参考記事

pipx

Why? pipx lets me install Python-based CLI stuff (youtube-dl, awscli, doc2dash, etc.) without those projects’ dependencies messing up my global Python.

Pythonはawscliなどのコマンドのインストールにも使うため、それと開発用のライブラリのバージョンが衝突したりするとけっこう大変です。

参考記事

日本語でざっくり使い方を知りたい方は、以下の記事を読むといいでしょう。

Poetry

Why? Poetry handles dependency- and virtual-environment-management in a way that’s very intuitive (to me), and fits perfectly with my desired workflow.

似たような用途でPipenvがありますが、私も最近はPoetryを使っています。最近HackerNewsでも「Pipenvって最近ちゃんとメンテナンスされてなくない?」という趣旨の話題が出てました。

  • Pipenvは .python-version を読み込んでpyenvのバージョン切り替えを自動行うが、Poetryは手動で行う必要がある
  • Poetryにはパッケージとしてビルドして、PyPIに公開することがコマンドがある

また、以前はPoetryには requirements.txt でのエクスポートコマンドがなかったのですが、最近実装されていました。Dockerを利用する際はこのコマンドで生成した requirements.txt を、Dockerイメージ内で pip install -r requirements.txt するようDockerfileを書くのがいいでしょう。

poetry export -f requirements.txt > requirements.txt

参考記事

18
20
1

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
18
20