Hatchを使ってsetup.pyをpyproject.tomlに移行する#

Hatch can be useful for generating your project's pyproject.toml file if your project already has a setup.py file.

注釈

This step is not necessary and is only helpful if your project already has a setup.py file defined.

学習目標

In this lesson, you will learn:

  1. すでに setup.py が定義されているプロジェクトで pyproject.toml を使用するように移行するために Hatch を使用するプロセスです。

Hatchとは何か?#

HatchはPythonパッケージマネージャで、Pythonパッケージの作成、管理、配布のプロセスを効率化するように設計されています。 新しいプロジェクトの作成、依存関係の管理、ディストリビューションのビルド、PyPI のようなリポジトリへのパッケージの公開といったタスクのための便利な CLI (Command-Line Interface) を提供します。

Hatchを知る

See Get to know Hatch for more information.

前提条件#

始める前に、システムにHatchがインストールされていることを確認してください。pipでインストールできます:

pipx install hatch

サンプルディレクトリツリー#

それでは、 hatch init を使う前と後のディレクトリツリー構造のサンプルを見てみましょう:

hatch init#

project/
│
├── src/
│   └── my_package/
│       ├── __init__.py
│       └── module.py
│
├── tests/
│   └── test_module.py
│
└── setup.py

hatch init#

project/
│
├── pyproject.toml
│
├── src/
│   └── my_package/
│       ├── __init__.py
│       └── module.py
│
├── tests/
│   └── test_module.py
│
└── setup.py

ご覧のように、 hatch init を実行した後の主な変化は、プロジェクトディレクトリに pyproject.toml ファイルが追加されたことです。

ステップバイステップガイド#

では、Hatchを使ってプロジェクトの pyproject.toml ファイルを作成する手順を説明しましょう。

  1. プロジェクトディレクトリに移動する: ターミナルかコマンドプロンプトを開き、Pythonプロジェクトがあるディレクトリに移動します。

  2. Hatchの初期化: 以下のコマンドを実行し、プロジェクトディレクトリのHatchを初期化します:

    hatch new --init
    
  3. Review and Customize: After running the previous command, Hatch will automatically generate a pyproject.toml file based on your existing project configuration. Take some time to review the contents of the generated pyproject.toml file. You may want to customize certain settings or dependencies based on your project's requirements (see pyproject.toml tutorial for more information about the pyproject.toml).

  4. Verify: Verify that the pyproject.toml file accurately reflects your project configuration and dependencies. You can manually edit the file, but be cautious and ensure that the syntax is correct.

  5. setup.py を削除する: pyproject.toml のみを使用するように移行するので、 setup.py ファイルは不要になります。 プロジェクトディレクトリから安全に削除できます。

  6. テストビルド: 先に進む前に、 pyproject.toml ファイルだけを使ってプロジェクトが正常にビルドされることを確認する必要がある。 以下のコマンドを実行してプロジェクトをビルドします:

hatch build

このコマンドは pyproject.toml ファイルの仕様に基づいてプロジェクトをビルドします。 ビルドの過程でエラーや警告がないか、必ず確認してください。

  1. 既存機能のテスト: pyproject.toml でプロジェクトのビルドに成功したら、 プロジェクトの既存の機能が損なわれないようにすることが重要です。既存のテストを実行し、すべてが期待通りに動作することを確認します。