pypa / hatch

Modern, extensible Python project management
https://hatch.pypa.io/latest/
MIT License
5.67k stars 277 forks source link

Environment Variables for all environments #1362

Open kassett opened 3 months ago

kassett commented 3 months ago

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

ofek commented 3 months ago

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

kassett commented 3 months ago

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

ofek commented 3 months ago

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

kassett commented 2 months ago

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?

ofek commented 2 months ago

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

kassett commented 2 months ago

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.

ofek commented 2 months ago

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

kassett commented 2 months ago
[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.

ofek commented 2 months ago

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

kassett commented 2 months ago

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 commented 2 months ago

Hey Ofek,

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

ofek commented 2 months ago

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]
kassett commented 2 months ago

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

demizer commented 1 month ago

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"
ofek commented 1 month ago

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.