hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Environment Variables for all environments

Open kassett opened this issue 1 year ago • 15 comments

Can we add support for adding environment variables to all environments in a central place?

kassett avatar Apr 03 '24 20:04 kassett

You can have a base environment that you can use as a template that all environments may inherit from.

ofek avatar Apr 03 '24 20:04 ofek

Is there documentation for this -- I haven't seen this yet?

kassett avatar Apr 03 '24 20:04 kassett

https://hatch.pypa.io/latest/config/environment/overview/#inheritance

ofek avatar Apr 03 '24 21:04 ofek

Hey Ofek So actually I think that this doesn't exactly solve my use case. I want to set PYTHONPATH in each environment. Using a template is great, but that template cannot apply to the default environment, which is really a shame. Any ideas on how to fix this?

kassett avatar Apr 08 '24 17:04 kassett

Can you give me two example environments and the expected value of that variable?

ofek avatar Apr 08 '24 18:04 ofek

Yes of course. I have a default environment, which I would like to include the lint and test features, and then I have lint and test environments, which have different dependencies. I would like all of these environment export a PYTHONPATH and a few other environment variables. I could use the default environment as a template, but then I would be importing features that I don't want.

kassett avatar Apr 08 '24 19:04 kassett

I'm trying to understand what the value of that variable would be in order to provide you accurate configuration.

ofek avatar Apr 08 '24 19:04 ofek

[tool.hatch.envs.default]
dev-mode = true
python = "3.12"
features = [
    "dev"
]

[tool.hatch.envs.default.env-vars]
PYTHONPATH = "backend"

[tool.hatch.envs.deploy]
dev-mode = true
python = "3.12"

In this case, I would like the PYTHONPATH to be exported in all environments AND I would like to use the default environment as an environment that has ALL features. Having a template that could be used for the default environment seems like the best course of action.

kassett avatar Apr 08 '24 19:04 kassett

There is also a feature request to support .env files; would that help?

ofek avatar Apr 08 '24 20:04 ofek

Yeah I mean I'm specifically trying to avoid that, I think a more elegant solution would be something like

[tool.hatch.envs.*.env-vars]

kassett avatar Apr 08 '24 21:04 kassett

Hey Ofek,

Was wondering what you think of this feature? I can add it.

kassett avatar Apr 12 '24 13:04 kassett

I would love that, thanks! It would be part of this configuration table: https://github.com/pypa/hatch/blob/a27473d478299060327b52a85c22aa038171c376/src/hatch/project/config.py#L37

[tool.hatch.env]

ofek avatar Apr 12 '24 13:04 ofek

What do you think of this? https://github.com/pypa/hatch/pull/1390 Or do you want just a "env-vars" under tool.hatch.env

kassett avatar Apr 15 '24 12:04 kassett

It would also be great if the environment variables applied to the hatch-test env. It is currently not possible to set PIP_EXTRA_INDEX_URL for the hatch test command, which seems to ignore the variable set in the global environment.

The workaround is

[tool.hatch.envs.test]
description = "Testing with pytest"
type = "pip-compile"
lock-filename = "requirements/requirements-test-lock.txt"
template = "hatch-test"

[tool.hatch.envs.test.env-vars]
PIP_EXTRA_INDEX_URL = "https://privaterepo.com/simple"

demizer avatar May 29 '24 21:05 demizer

The test environment does not inherit from anything so you must put all of your configuration within that i.e. in your example change the name to hatch-test and it should work.

ofek avatar May 29 '24 21:05 ofek