Environments¶環境¶
Environments are designed to allow for isolated workspaces for testing, building documentation, or anything else projects need.環境は、テスト、ドキュメントの作成、またはプロジェクトが必要とするその他の作業のための孤立したワークスペースを提供するように設計されています。
Unless an environment is chosen explicitly, Hatch will use the default environment.環境が明示的に選択されない限り、Hatchはdefault環境を使用します。
Tipヒント
For a more comprehensive walk-through, see the Basic usage tutorial.より包括的な手順については、基本的な使用法チュートリアルを参照してください。
Creation¶作成¶
You can create environments by using the env create command. Let's enter the directory of the project we created in the setup phase:環境は、env createコマンドを使用して作成できます。セットアップフェーズで作成したプロジェクトのディレクトリに移動しましょう:
$ hatch env create
Creating environment: default
Installing project in development mode
Syncing dependencies
Tipヒント
You never need to manually create environments as spawning a shell or running commands within one will automatically trigger creation.シェルを生成するやコマンドを実行することで、環境を手動で作成する必要はありません。これにより、自動的に作成がトリガーされます。
Entering environments¶環境への入場¶
You can spawn a shell within an environment by using the shell command.環境内でシェルを生成するには、shellコマンドを使用します。
$ hatch shell
(hatch-demo) $
Now confirm the project has been installed:プロジェクトがインストールされていることを確認してください:
(hatch-demo) $ pip show hatch-demo
Name: hatch-demo
Version: 0.0.1
...
Finally, see where your environment's Python is located:最後に、あなたの環境のPythonがどこにあるかを確認してください:
(hatch-demo) $ python -c "import sys;print(sys.executable)"
...
You can type exit to leave the environment.exitと入力して環境を離れることができます。
Command execution¶コマンド実行¶
The run command allows you to execute commands in an environment as if you had already entered it. For example, running the following command will output the same path as before:runコマンドを使用すると、すでにその環境に入っているかのようにコマンドを実行できます。たとえば、次のコマンドを実行すると、以前と同じパスが出力されます:
hatch run python -c "import sys;print(sys.executable)"
Tipヒント
Be sure to check out how to define scripts for your project.プロジェクトのためにスクリプトを定義する方法を必ず確認してください。
Dependencies¶依存関係¶
Hatch ensures that environments are always compatible with the currently defined project dependencies (if installed and in dev mode) and environment dependencies.Hatchは、環境が現在定義されているプロジェクトの依存関係(インストールされている場合および開発モードの場合)および環境の依存関係と常に互換性があることを保証します。
To add cowsay as a dependency, open pyproject.toml and add it to the dependencies array:cowsayを依存関係として追加するには、pyproject.tomlを開き、dependencies配列に追加します:
[project]
...
dependencies = [
"cowsay"
]
This dependency will be installed the next time you spawn a shell or run a command. For example:この依存関係は、次回シェルを生成するか、コマンドを実行する際にインストールされます。たとえば:
$ hatch run cowsay -t "Hello, world!"
Syncing dependencies
_____________
| Hello, world! |
=============
\
\
^__^
(oo)\_______
(__)\ )\/\
||----w |
|| ||
Note注意
The Syncing dependencies status will display temporarily when Hatch updates environments in response to any dependency changes that you make.依存関係の同期中のステータスは、Hatchがあなたの行った依存関係の変更に応じて環境を更新している間、一時的に表示されます。
Selection¶選択¶
You can select which environment to enter or run commands in by using the -e/--env root option or by setting the HATCH_ENV environment variable.-e/--env ルートオプションを使用するか、HATCH_ENV環境変数を設定することで、どの環境に入るか、またはコマンドを実行するかを選択できます。
The run command allows for more explicit selection by prepending <ENV_NAME>: to commands. For example, if you had the following configuration:run コマンドは、コマンドの前に <ENV_NAME>: を付けることで、より明示的な選択を可能にします。例えば、次のような設定があった場合:
[tool.hatch.envs.docs]
dependencies = [
"mkdocs"
]
[tool.hatch.envs.docs.scripts]
build = "mkdocs build --clean --strict"
serve = "mkdocs serve --dev-addr localhost:8000"
[envs.docs]
dependencies = [
"mkdocs"
]
[envs.docs.scripts]
build = "mkdocs build --clean --strict"
serve = "mkdocs serve --dev-addr localhost:8000"
you could then serve your documentation by running:その後、次のコマンドを実行することでドキュメントを提供できます:
hatch run docs:serve
Tipヒント
If you've already entered an environment, commands will target it by default.すでに環境に入っている場合、コマンドはデフォルトでそれをターゲットにします。
Matrix¶Matrix¶
Every environment can define its own set of matrices:各環境は独自のマトリックスを定義できます:
[tool.hatch.envs.test]
dependencies = [
"pytest"
]
[[tool.hatch.envs.test.matrix]]
python = ["3.10", "3.11"]
version = ["42", "3.14"]
[[tool.hatch.envs.test.matrix]]
python = ["3.11", "3.12"]
version = ["9000"]
feature = ["foo", "bar"]
[envs.test]
dependencies = [
"pytest"
]
[[envs.test.matrix]]
python = ["3.10", "3.11"]
version = ["42", "3.14"]
[[envs.test.matrix]]
python = ["3.11", "3.12"]
version = ["9000"]
feature = ["foo", "bar"]
Using the env show command would then display:env show コマンドを使用すると、次のように表示されます:
$ hatch env show --ascii
Standalone
+---------+---------+
| Name | Type |
+=========+=========+
| default | virtual |
+---------+---------+
Matrices
+------+---------+----------------------+--------------+
| Name | Type | Envs | Dependencies |
+======+=========+======================+==============+
| test | virtual | test.py3.10-42 | pytest |
| | | test.py3.10-3.14 | |
| | | test.py3.11-42 | |
| | | test.py3.11-3.14 | |
| | | test.py3.11-9000-foo | |
| | | test.py3.11-9000-bar | |
| | | test.py3.12-9000-foo | |
| | | test.py3.12-9000-bar | |
+------+---------+----------------------+--------------+
Removal¶削除¶
You can remove a single environment or environment matrix by using the env remove command or all of a project's environments by using the env prune command.単一の環境または環境マトリックスを削除するには、env remove コマンドを使用するか、プロジェクトのすべての環境を削除するには、env prune コマンドを使用します。