Skip to content

Managing environments環境の管理


Hatch environments are isolated workspaces that can be used for project tasks including running tests, building documentation and running code formatters and linters.Hatch 環境は、テストの実行、ドキュメントのビルド、コードフォーマッタやリンターの実行など、プロジェクトタスクに使用できる孤立した作業スペースです。

The default environmentデフォルト環境

When you start using Hatch, you can create the default environment. To do this use the env create command:Hatchを使用し始めると、default環境を作成できます。これを行うには、env createコマンドを使用します:

hatch env create

This will not only create will the default environment for you to work in but will also install your project in dev mode in this default environment.これにより、作業用のdefault環境が作成されるだけでなく、このdefault環境にプロジェクトがdev modeでインストールされます。

Tipヒント

You never need to manually create environments as spawning a shell or running commands within one will automatically trigger creation.手動で環境を作成する必要はありません。特定の環境内でシェルを起動する特定の環境内でコマンドを実行することで、自動的に作成がトリガーされます。

Using the default environmentデフォルト環境の使用

Hatch will always use the default environment if an environment is not chosen explicitly when running a command.Hatchは、コマンドを実行する際に明示的に環境が選択されていない場合、常にdefault環境を使用します。

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デフォルト環境の設定

You can customize the tools that are installed into the default environment by adding a table called tool.hatch.envs.default to your pyproject.toml file. Below is an example of adding the dependencies pydantic and numpy to the default environment.pyproject.tomlファイルにtool.hatch.envs.defaultというテーブルを追加することで、default環境にインストールされるツールをカスタマイズできます。以下は、依存関係としてpydanticnumpydefault環境に追加する例です。

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

You can declare versions for your dependencies as well within this configuration.この設定内で依存関係のバージョンを宣言することもできます。

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

Create custom environmentカスタム環境を作成する

You can create custom environments in Hatch by adding a section to your pyproject.toml file [tool.hatch.envs.<ENV_NAME>]. Below you define an environment called test and you add the pytest and pytest-cov dependencies to that environment's configuration.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. Create the environment環境を作成する
  2. Install your project into that environment in dev mode (by default) along with its dependencies.その環境にあなたのプロジェクトをdev mode(デフォルト)でインストールし、その依存関係もインストールします。
  3. Install the environment's dependencies環境の依存関係をインストールする

Run commands within a specific environment特定の環境内でコマンドを実行する

Hatch offers a unique environment feature that allows you run a specific command within a specific environment rather than needing to activate the environment as you would using a tool such as Conda or venv.Hatchは、Condavenvのようなツールを使用して環境をアクティブにする必要なく、特定の環境内で特定のコマンドを実行できるユニークな環境機能を提供します。

For instance, if you define an environment called test that contains the dependencies from the previous section, you can run the pytest command from the test environment using the syntax:たとえば、前のセクションからの依存関係を含むtestという環境を定義した場合、次の構文を使用してtest環境からpytestコマンドを実行できます:

hatch run <ENV_NAME>:command

To access the test environment and run pytest, you can run: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 represents the name of the environment to call (test) and the command to run (pytest).test:pytestは、呼び出す環境の名前(test)と実行するコマンド(pytest)を表します。

View current environments現在の環境を表示

Above you defined and created a new test environment in your pyproject.toml file. You can now use the env show command to see both the currently created environments and the dependencies in each environment.上記では、pyproject.tomlファイルに新しいテスト環境を定義して作成しました。現在、env showコマンドを使用して、現在作成された環境と各環境の依存関係を確認できます。

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

Note注意

The output may have more columns depending on your environment configuration.出力には、環境設定に応じてさらに多くの列が含まれる場合があります。

Locating environments環境の場所を特定

To see where your current environment is located you can use the env find command.現在の環境がどこにあるかを確認するには、env findコマンドを使用できます。

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

Note注意

That path is what you would see on macOS but differs for each platform, and is configurable.そのパスはmacOSで見ることができるものですが、各プラットフォームで異なり、設定可能です。

Launching a shell within a specific environment特定の環境内でシェルを起動

If you wish to launch a shell for a specific environment that you have created, like the previous test environment, you can use:作成した特定の環境、例えば前述のtest環境のためにシェルを起動したい場合、次のように使用できます:

hatch -e test shell

Once the environment is active, you can run commands like you would in any Python environment.環境がアクティブになると、任意のPython環境で行うようにコマンドを実行できます。

Notice below that when running pip list in the test environment, you can see:テスト環境で pip list を実行すると、以下のように表示されます:

  1. That your package is installed in editable mode.パッケージが編集可能モードでインストールされていること。
  2. That the environment contains both pytest and pytest-cov as specified above in the pyproject.toml file.環境には、上記の 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 environmentsConda環境

If you prefer to use Conda environments with Hatch, you can check out the hatch-conda plugin.HatchでConda環境を使用したい場合は、hatch-condaプラグインをチェックしてください。