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.
If your project does not already define a
setup.pysee Make your Python code installable
学習目標
In this lesson, you will learn:
すでに
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 ファイルを作成する手順を説明しましょう。
プロジェクトディレクトリに移動する: ターミナルかコマンドプロンプトを開き、Pythonプロジェクトがあるディレクトリに移動します。
Hatchの初期化: 以下のコマンドを実行し、プロジェクトディレクトリのHatchを初期化します:
hatch new --init
Review and Customize: After running the previous command, Hatch will automatically generate a
pyproject.tomlfile based on your existing project configuration. Take some time to review the contents of the generatedpyproject.tomlfile. You may want to customize certain settings or dependencies based on your project's requirements (see pyproject.toml tutorial for more information about thepyproject.toml).Verify: Verify that the
pyproject.tomlfile accurately reflects your project configuration and dependencies. You can manually edit the file, but be cautious and ensure that the syntax is correct.setup.py を削除する:
pyproject.tomlのみを使用するように移行するので、setup.pyファイルは不要になります。 プロジェクトディレクトリから安全に削除できます。テストビルド: 先に進む前に、
pyproject.tomlファイルだけを使ってプロジェクトが正常にビルドされることを確認する必要がある。 以下のコマンドを実行してプロジェクトをビルドします:
hatch build
このコマンドは pyproject.toml ファイルの仕様に基づいてプロジェクトをビルドします。 ビルドの過程でエラーや警告がないか、必ず確認してください。
既存機能のテスト:
pyproject.tomlでプロジェクトのビルドに成功したら、 プロジェクトの既存の機能が損なわれないようにすることが重要です。既存のテストを実行し、すべてが期待通りに動作することを確認します。