Pythonとパッケージングと私

782 views

Published on

pyconjp 2017 発表資料

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
782
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Pythonとパッケージングと私

  1. 1. Preface setup.cfg pyproject.toml conclusion Pythonとパッケージングと私 Atsushi Odagiri 2017-09-08
  2. 2. Preface setup.cfg pyproject.toml conclusion お前誰よ • Atsushi Odagiri • Open Collector, Inc. • Repoze/Pylons/Pyramid
  3. 3. Preface setup.cfg pyproject.toml conclusion パッケージングの基本 • pypi パッケージリポジトリ • pip インストーラー • setuptools/wheel パッケージャー • virtualenv 仮想環境
  4. 4. Preface setup.cfg pyproject.toml conclusion pypi • pypi.python.org - 現在 • pypi.org - 次バージョン • 徐々に pypi.org ドメインに機能移動
  5. 5. Preface setup.cfg pyproject.toml conclusion pip • sdist や wheel をリポジトリからダウンロードしてイン ストールなど • インストールされてるパッケージの状況を取得など • pip 9.0.1
  6. 6. Preface setup.cfg pyproject.toml conclusion setuptools/wheel • setup.py から wheel を作る • setuptools 36.4.0 • wheel 0.29.0
  7. 7. Preface setup.cfg pyproject.toml conclusion virtualenv • ライブラリのインストール先をプロジェクトごとに 分離 • virtualenv 15.1.0 • pip 9.0.1 • setuptools 28.0.0
  8. 8. Preface setup.cfg pyproject.toml conclusion pipとvertualenvの使い方 $ virtualenv venv $ . venv/bin/activate (venv)$ pip install -U pip setuptools wheel (venv)$ cat > requirements.txt pyramid ˆD (venv)$ pip install -r requirements.txt (venv)$ pip list –format=columns Package Version ————– ——- Pyramid 1.9
  9. 9. Preface setup.cfg pyproject.toml conclusion wheelhouseにバンドル (venv)$ pip freeze > constraints.txt (venv)$ mkdir wheelhouse (venv)$ pip wheel -r requirements.txt -c constraints.txt -w wheelhouse -f wheelhouse (venv)$ deactivate $ virtualenv venv2 $ . venv2/bin/activate (venv2)$ pip install -r requirements.txt -c constraints.txt –no-index -f wheelhouse
  10. 10. Preface setup.cfg pyproject.toml conclusion パッケージを作るには? • setuptools を使う • setup.py を書く • setup.py で bdist_wheel コマンドを実行する • setup.py upload か twine で pypi にアップロードする (事前にアカウント作成必要)
  11. 11. Preface setup.cfg pyproject.toml conclusion setup.py from setuptools import setup, find_packages import sample setup( name="sample-package", version=sample.version, author="Atsushi Odagiri", author_email="aodagx@gmail.com", description="sample package to use setup.py", long_description=""" """, url="https://aodag.jp/sample-package", license="MIT", packages=find_packages(), )
  12. 12. Preface setup.cfg pyproject.toml conclusion setup.pyのだめなとこ • 設定と実行コードが混在 • long_description を外部ファイルに書くことが多い • ファイル指定などは提供されてない • みんなそれぞれ setup.py の中でファイルを読み込む処 理を書いてる • パッケージのメタデータとして扱われる項目と setuptools が利用する項目が分かれてない
  13. 13. Preface setup.cfg pyproject.toml conclusion setup.cfg にパッケージメタデータを書こう • setuptools 30.3.0 から setup.cfg にメタデータを書ける ようになった • metadata セクションと options セクション に書く • ほぼ setup 関数の引数のまま
  14. 14. Preface setup.cfg pyproject.toml conclusion setup.cfg にメタデータを書いた場合の setup.py from setuptools import setup setup() シンプル!
  15. 15. Preface setup.cfg pyproject.toml conclusion metadataセクションの主な項目 • name • version • author • author_email • description • long_description • url • lisence • classifiers
  16. 16. Preface setup.cfg pyproject.toml conclusion 実際のsetup.cfg(metadataセクション) [metadata] name = sample-package version = attr:sample.version author = Atsushi Odagiri author_email = aodagx@gmail.com description = sample package to use setup.cfg long_description = file:README.rst url = https://aodag.jp/sample-package license = MIT
  17. 17. Preface setup.cfg pyproject.toml conclusion 特殊な項目 • version • 直接書いてもいいが attr: でオブジェクトの内容を利用 できる • callable な場合は評価された結果がバージョンになる • 実行されてしまうので import するだけで副作用が起き るコードは要注意 • long_description • 直接書いてもいいが file: で指定したファイルの内容を 利用できる • 今のところ 1 ファイルしか指定できない
  18. 18. Preface setup.cfg pyproject.toml conclusion optionsセクションの主な項目 • packages • install_requires • entry_points
  19. 19. Preface setup.cfg pyproject.toml conclusion options.* なセクション • options.extras_require • options.packages.find
  20. 20. Preface setup.cfg pyproject.toml conclusion 実際のsetup.cfg(optionsセクション) [options] install_requires = pyramid sqlalchemy jinja2 packages = find: entry_points = file:entry_points.cfg
  21. 21. Preface setup.cfg pyproject.toml conclusion 実際のsetup.cfg(options.* セクション) [options.extras_require] testing = pytest mysql = pymysql postgres = psycopg2 [options.packages.find] exclude = tests examples
  22. 22. Preface setup.cfg pyproject.toml conclusion options の特殊な項目 • packages • 自分で全部羅列してもよい • find_packages 相当のことをする場合、 find: とだけ指 定して options.packages でオプション指定する • entry_points • 別途 entry_points を記述したファイルを指定するか options.entry_points セクションで指定する
  23. 23. Preface setup.cfg pyproject.toml conclusion entry_pointsのファイル [console_scripts] hello = sample:hello
  24. 24. Preface setup.cfg pyproject.toml conclusion options.entry_points での指定方法 [options.entry_points] console_scripts = hello=sample:hello
  25. 25. Preface setup.cfg pyproject.toml conclusion pbrとの違い • pbr は setuptools の拡張 • openstack プロジェクトがパッケージングのために作成 した • setup.cfg にメタデータを記述する • その他 git タグによるバージョニングなど • setup.cfg の項目がちょっと違う
  26. 26. Preface setup.cfg pyproject.toml conclusion バージョンをgitタグから自動で設定したい setuptools_scm を使うと git タグからバージョンを生成で きる setup( name="example", setup_require=["setuptools_scm"], use_scm_version=True, )
  27. 27. Preface setup.cfg pyproject.toml conclusion setup.cfgでsetuptools_scmを試してみる version = attr:setuptools_scm.get_version • callable を指定すると評価結果がバージョンになる • 実行時に setuptools_scm が入ってないといけない • setuptools_scm が必要だということは setup_requires で指定できるが…
  28. 28. Preface setup.cfg pyproject.toml conclusion setup.cfg の setup_requires • setup.cfg に書いたのではもう遅い • setup 関数に書くしかない? setup(setup_requires=["setuptools_scm"])
  29. 29. Preface setup.cfg pyproject.toml conclusion setuptoolsの機能追加や拡張など • setuptools のバージョンが古いと setup.cfg の機能が使 えない • ユーザーの setuptools のバージョンは不確定 • setup_requires のタイミングは微妙 • 結局 setuptools の実装に頼っている部分があまり明確に なってない
  30. 30. Preface setup.cfg pyproject.toml conclusion build-system • setuptools 以外のビルドツールを指定できる • setuptools を使う場合でも必要な拡張やバージョンを指 定できる
  31. 31. Preface setup.cfg pyproject.toml conclusion PEP 518 • pyproject.toml でビルドツールを指定する • 存在しなければ今までの setuptools によるビルド
  32. 32. Preface setup.cfg pyproject.toml conclusion pyproject.toml の形式 ビルドツールに flit を使う場合の例 [build-system] requires = ["flit"] backend = "flit.buildapi"
  33. 33. Preface setup.cfg pyproject.toml conclusion ビルドツールの実装について • PEP516 • コマンドラインでの実装 • reject されました • PEP517 • API での実装 • 議論中
  34. 34. Preface setup.cfg pyproject.toml conclusion PEP517で定義されてるAPI • build_wheel • build_sdist • optional get_requires_for_build_wheel • optional prepare_metadata_for_build_wheel • optional get_requires_for_build_sdist
  35. 35. Preface setup.cfg pyproject.toml conclusion pipのPEP518,PEP517対応 • pyproject.tom の読み込み [PEP518] はマージされている • setup_requires の問題は解決できそう • 隔離された環境での wheel 生成 • まだツールは setuptools 固定 [PEP517 未対応]
  36. 36. Preface setup.cfg pyproject.toml conclusion flit: setuptools以外のパッケージングツール • flit は setuptools とは別のパッケージングツール • filit.ini でメタデータを書く • シンプル
  37. 37. Preface setup.cfg pyproject.toml conclusion flit を使うには $ pip install flit $ flit init $ flit wheel
  38. 38. Preface setup.cfg pyproject.toml conclusion flit の PEP517対応 • toml-config ブランチ • flit.buildapi
  39. 39. Preface setup.cfg pyproject.toml conclusion flitを使うべきか? • pip の対応ができてないと sdist としてインストールで きない • wheel だけで配布するというのなら今からでも可能 • setuptools 自体も setup.cfg への移行など進んでる • 個人的にはオルタナティブなツールは歓迎だが、使う かっていうと…
  40. 40. Preface setup.cfg pyproject.toml conclusion 話し足りないこと • プライベートパッケージリポジトリ • Windows での C 拡張 • pip へのコントリビュート • conda2wheel
  41. 41. Preface setup.cfg pyproject.toml conclusion まとめ • setuptools ちょっとだけ進化 • sdist から wheel を作る流れが PEP で定義される • setuptools 使わなくてもよい未来

×
Save this presentationTap To Close