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
環境にインストールされるツールをカスタマイズできます。以下は、dependenciespydantic
とnumpy
をdefault
環境に追加する例です。
[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
という環境を定義し、その環境の設定にpytest
とpytest-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は次のことを行います。:
- 環境の作成
- dev mode(デフォルト)で、dependenciesとともにプロジェクトをその環境にインストールします。
- 環境のdependenciesをインストールします。
Run commands within a specific environment¶
Hatchには、Condaやvenvなどのツールを使用する場合のように環境をアクティブにする必要がなく、特定の環境内で特定のコマンドを実行できる独自の環境機能があります。
たとえば、前のセクションの依存関係を含む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
を実行すると、次のように表示されることに注意してください。:
- パッケージが編集可能モードでインストールされていること。
- 上記の
pyproject.toml
ファイルで指定されているように、環境にpytest
とpytest-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をチェックしてください。