Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment Variables for all environments #1362

Open
kassett opened this issue Apr 3, 2024 · 15 comments
Open

Environment Variables for all environments #1362

kassett opened this issue Apr 3, 2024 · 15 comments

Comments

@kassett
Copy link

kassett commented Apr 3, 2024

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

@ofek
Copy link
Sponsor Collaborator

ofek commented Apr 3, 2024

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

@kassett
Copy link
Author

kassett commented Apr 3, 2024

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

@ofek
Copy link
Sponsor Collaborator

ofek commented Apr 3, 2024

@kassett
Copy link
Author

kassett commented Apr 8, 2024

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
Copy link
Sponsor Collaborator

ofek commented Apr 8, 2024

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

@kassett
Copy link
Author

kassett commented Apr 8, 2024

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
Copy link
Sponsor Collaborator

ofek commented Apr 8, 2024

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

@kassett
Copy link
Author

kassett commented Apr 8, 2024

[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
Copy link
Sponsor Collaborator

ofek commented Apr 8, 2024

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

@kassett
Copy link
Author

kassett commented Apr 8, 2024

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
Copy link
Author

kassett commented Apr 12, 2024

Hey Ofek,

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

@ofek
Copy link
Sponsor Collaborator

ofek commented Apr 12, 2024

I would love that, thanks! It would be part of this configuration table:

def env(self):

[tool.hatch.env]

@kassett
Copy link
Author

kassett commented Apr 15, 2024

What do you think of this?
#1390
Or do you want just a "env-vars" under tool.hatch.env

@demizer
Copy link

demizer commented May 29, 2024

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
Copy link
Sponsor Collaborator

ofek commented May 29, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants