Upgrade to Pro — share decks privately, control downloads, hide ads and more …

データ分析ツール開発でpoetryを使う選択肢

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for mizzsugar mizzsugar
February 20, 2021

 データ分析ツール開発でpoetryを使う選択肢

Avatar for mizzsugar

mizzsugar

February 20, 2021
Tweet

More Decks by mizzsugar

Other Decks in Programming

Transcript

  1. 自己紹介 • みずきと申します。 • Twitter → @mizzsugar0425 • 普段はPythonを使ってWEBサービスの開発をしています。 •

    Spready株式会社所属 • myProduct株式会社で開発のお手伝いもしています。 • 今回のお話はmyProduct株式会社での事例です。 • 発表中のソースコードは、実際のプロジェクトで利用したものを一部改変していま す。 • 分析の内容や手法の話はしません。 2
  2. ディレクトリ構成 ── project ├── application (CRONやデータベースと接続する部分 ) │ ├── application

    │ │ ├── __init__.py │ ├── poetry.lock │ ├── pyproject.toml ├── analysis (分析部分) ├── analysis │ ├── __init__.py ├── poetry.lock └── pyproject.toml 5
  3. project/application/application/__init__.py import pathlib import tempfile import analysis # 分析のパッケージ def

    report(parameter: analysis.Parameter) -> None: with tempfile.TemporaryDirectory() as d: input_data = ... # 分析対象となるデータを抽出する output_path = pathlib.Path(d) analysis.report(input_data, parameter, output_path) ... 6
  4. setup.cfg [metadata] name = analysis version = 1.0.0 description =

    this is a sample library long_description = README.rst license = BSD 3-Clause License [options] install_requires = pandas python_requires='>=3.7' [options.extras_require] develop = flake8 mypy 9 各 項 目 の 意 味 はこちら ↓ https://packaging.python.org/guides/distributing-packages-using-setuptools/
  5. $ poetry init This command will guide you through creating

    your pyproject.toml config. Package name [analysis]: analysis Version [0.1.0]: Description []: library for analysis Author [mizzsugar <[email protected]>, n to skip]: mizzsugar <[email protected]> License []: BSD-3-Clause Compatible Python versions [^3.8]: ^3.7 Would you like to define your main dependencies interactively? (yes/no) [yes] yes You can specify a package in the following forms: - A single name (requests) - A name and a constraint (requests@^2.23.0) - A git url (git+https://github.com/python-poetry/poetry.git) - A git url with a revision (git+https://github.com/python-poetry/poetry.git#develop) - A file path (../my-package/my-package.whl) - A directory (../my-package/) - A url (https://example.com/packages/my-package-0.1.0.tar.gz) Search for package to add (or leave blank to continue): pandas Adding pandas tqdm Add a package: Would you like to define your development dependencies interactively? (yes/no) [yes] no Generate 13 ※オレンジ色の枠は回答する箇所です。 poetry add [パッケージ名] で後から依存パッケージを 増やせます。
  6. project/analysis/pyproject.toml [tool.poetry] name = "analysis" version = "0.1.0" description =

    "library for analysis" authors = ["mizzsugar <[email protected]>"] [tool.poetry.dependencies] python = "^3.7" pandas = "^1.1.3" [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api" 14
  7. pipを使ってanalysisをapplicationにインストール >> cd ~/project/application >> pip install ../analysis 15 ※ただし、pip

    install -e ../analysis を実行すると以下のエラーになります。 A "pyproject.toml" file was found, but editable mode currently requires a setup.py based build. 現段階では、pyproject.tomlは編集可能モードでのインストールを pipでサポートされていません。 (pipのバー ジョンが21.0.1時点の情報です) 編集可能モードでインストールしたい場合は、 applicationでもpoetryを使う必要があります。
  8. project/application/pyproject.toml [tool.poetry] name = "application" version = "0.1.0" description =

    "" authors = ["mizzsugar"] [tool.poetry.dependencies] python = "^3.7" analysis = {path = "../analysis"} fastapi = {version = "^0.61.1"} uvicorn = {extras = ["standard"], version = "^0.12.2"} PyDrive = "^1.3.1" mysqlclient = "^2.0.1" [build-system] requires = ["poetry>=0.12"] build-backend = "poetry.masonry.api" https://python-poetry.org/docs/cli/ {path = "パス"} で指定したパスのパッケージを インストールします。 17
  9. Poetryでパッケージ化して良かった点 • setup.cfgを作らなくてよいのが楽でした。 (poetry init, poetry add するだけ!) • 分析の要望がガラっと変わって新しいパッケージを作成した際に、

    新旧パッケージがお互いにまったく依存しない状態に出来ました。 (applicationからimport analysisとしている箇所を消すだけ!) 18