Skip to content

Managing environments


Hatchenvironmentsは、テストの実行、ドキュメントの構築、コードフォーマッタとリンタの実行などのプロジェクトタスクに使用できる独立したワークスペースです。

The default environment

Hatchの使用を開始すると、 default環境を作成できます。これを行うには、env createコマンドを使用します。:

hatch env create

これにより、作業するための default環境が作成されるだけでなく、この default環境でプロジェクトがdev modeでインストールされます。

Tip

環境を手動で作成する必要はありません。シェル内のspawning a shellまたはrunning commandsによって自動的に作成がトリガーされます。

Using the default environment

Hatch will always use the default environment if an environment is not chosen explicitly when running a command.

running a command時に環境が明示的に選択されていない場合、Hatchは常にデフォルト環境を使用します。

For instance, the following shows how to get version information for the Python in use.

たとえば、次の例は、使用中のPythonのバージョン情報を取得する方法を示しています。

$ hatch run python -V
Python 3.12.1

Configure the default environment

pyproject.tomlファイルにtool.hatch.envs.defaultというテーブルを追加することで、default環境にインストールされるツールをカスタマイズできます。以下は、dependenciespydanticnumpydefault環境に追加する例です。

[tool.hatch.envs.default]
dependencies = [
  "pydantic",
  "numpy",
]
[envs.default]
dependencies = [
  "pydantic",
  "numpy",
]

この構成内で依存関係のバージョンを宣言することもできます。

[tool.hatch.envs.default]
dependencies = [
  "pydantic>=2.0",
  "numpy",
]
[envs.default]
dependencies = [
  "pydantic>=2.0",
  "numpy",
]

Create custom environment

Hatchでカスタム環境を作成するには、pyproject.tomlファイル[tool.hatch.envs.<ENV_NAME>]にセクションを追加します。その下にtestという環境を定義し、その環境の設定にpytestpytest-covの依存関係を追加します。

[tool.hatch.envs.test]
dependencies = [
  "pytest",
  "pytest-cov"
]
[envs.test]
dependencies = [
  "pytest",
  "pytest-cov"
]

The first time that you call the test environment, Hatch will:

テスト環境を初めて呼び出すと、Hatchは次のことを行います。:

  1. 環境の作成
  2. dev mode(デフォルト)で、dependenciesとともにプロジェクトをその環境にインストールします。
  3. 環境のdependenciesをインストールします。

Run commands within a specific environment

Hatchには、Condavenvなどのツールを使用する場合のように環境をアクティブにする必要がなく、特定の環境内で特定のコマンドを実行できる独自の環境機能があります。

たとえば、前のセクションの依存関係を含むtestという環境を定義した場合、次の構文を使用してtest環境からpytestコマンドを実行できます。:

hatch run <ENV_NAME>:command

test環境にアクセスしてpytestを実行するには、次のコマンドを実行します。:

$ hatch run test:pytest
============================== test session starts ===============================
platform darwin -- Python 3.12.1, pytest-7.4.4, pluggy-1.3.0
rootdir: /your/path/to/yourproject
collected 0 items

Note

test:pytestは、呼び出す環境の名前(test)と実行するコマンド(pytest)を表します。

View current environments

上記では、pyproject.tomlファイルに新しいテスト環境を定義して作成しました。これで、env showコマンドを使用して、現在作成されている環境と各環境の依存関係の両方を確認できます。

$ hatch env show
             Standalone
┏━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Name    ┃ Type    ┃ Dependencies ┃
┡━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━━━┩
│ default │ virtual │              │
├─────────┼─────────┼──────────────┤
│ test    │ virtual │ pytest       │
│         │         │ pytest-cov   │
└─────────┴─────────┴──────────────┘

Note

環境の構成によっては、出力に表示される列の数が多くなる場合があります。

Locating environments

現在の環境がどこにあるかを確認するには、env findコマンドを使用します。

$ hatch env find test
/your/path/Application Support/hatch/env/virtual/yourproject/twO2iQR3/test

Note

このパスはmacOSで表示されるものですが、プラットフォームごとに異なり、[configurable]です(../../plugins/environment/virtual.md#location)。

Launching a shell within a specific environment

前のtest環境のように、作成した特定の環境に対してlaunch a shellしたい場合は、次のコマンドを使用できます。:

hatch -e test shell

環境がアクティブになると、他のPython環境と同様にコマンドを実行できます。

テスト環境でpip listを実行すると、次のように表示されることに注意してください。:

  1. パッケージが編集可能モードでインストールされていること。
  2. 上記のpyproject.tomlファイルで指定されているように、環境にpytestpytest-covの両方が含まれていること。
$ pip list
Package     Version Editable project location
----------- ------- ----------------------------------------------------
coverage    7.4.1
iniconfig   2.0.0
packaging   23.2
pip         23.3.1
pluggy      1.4.0
pytest      8.0.0
pytest-cov  4.1.0
yourproject 0.1.0  /your/path/to/yourproject

Conda environments

HatchでConda環境を使用したい場合は、hatch-conda pluginをチェックしてください。