# Allow for overriding the default environment

**URL:** <https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581>\
**Category:** Packaging\
**Created:** [January 30, 2024, 7:10pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581 "2024-01-30T19:10:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![ofek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ofek/32/1033_2.png) [@ofek](https://discuss.python.org/u/ofek)\
**Post date:** [January 30, 2024, 7:10pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/1 "2024-01-30T19:10:42Z")

</div>

Would folks here generally be open to changes in [packaging/src/packaging/markers.py at 4d8534061364e3cbfee582192ab81a095ec2db51 · pypa/packaging · GitHub](https://github.com/pypa/packaging/blob/4d8534061364e3cbfee582192ab81a095ec2db51/src/packaging/markers.py#L175) such that an environment variable of JSON could determine the default environment? We have a great need for this at work to allow for cross compiling/resolution. Specifically, we are able to successfully cross compile using Zig but we are unable to resolve dependencies because of [Various package-index filtering flags do not affect the environment markers · Issue #11664 · pypa/pip · GitHub](https://github.com/pypa/pip/issues/11664). I think, however, even if that bug is fixed it would be useful to have a centralized place to explicitly define the environment since `packaging` is used by everything.

I’m willing to provide the PR of course 🙂

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 30, 2024, 7:20pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/2 "2024-01-30T19:20:39Z")

</div>

> [@ofek](#):
>
> an environment variable of JSON could determine the default environment

I would think you need a longer name then just `JSON` that make the meaning clear.  
Given that its for default environment then name the variable after its use not its encoding like: `PYTHON_PACKAGING_DEFAULT_ENVRONMENT`?

---

<div class="post-metadata">

**Author:** ![ofek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ofek/32/1033_2.png) [@ofek](https://discuss.python.org/u/ofek)\
**Post date:** [January 30, 2024, 7:22pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/3 "2024-01-30T19:22:27Z")

</div>

Sure, I was just talking about the format of the content not the name of the variable.

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [January 30, 2024, 8:06pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/4 "2024-01-30T20:06:02Z")

</div>

I’m not massively enthusiastic about the idea of an environment variable containing a JSON value. My main objections are usability and global impact.

The usability issue is that _setting_ such a variable is annoying - getting the quoting of a JSON value correct is really fiddly, especially of you’re working in a shell you’re not familiar with. Also, Windows doesn’t have the “temporary environment variable” feature from Unix of `ENV_VAR=xxx cmd`, so an environment variable that affects a single command is much harder to use.

As far as global impact is concerned, I don’t like the idea that setting something in your environment can change the behaviour of any tool that uses `packaging`, even those you didn’t realise used it. I can easily imagine bug reports along the lines of “pip didn’t install my dependency”, which turn out to be because the user set a different environment via a variable they’d forgotten. Such “action at a distance” makes for difficult-to-track bugs.

Having said this, I _am_ in favour of having an API in `packaging` that can be used to configure the default environment, so that there’s a way for applications like pip to change it _without_ needing to pass an explicit environment to every call that evaluates a marker.

> [@ofek](#):
>
> I’m willing to provide the PR of course 🙂

Would you instead be willing to provide a fix for [Various package-index filtering flags do not affect the environment markers · Issue #11664 · pypa/pip · GitHub](https://github.com/pypa/pip/issues/11664)? It seems like that would be less controversial, and from what you say would also address your issue.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [January 30, 2024, 8:23pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/5 "2024-01-30T20:23:08Z")

</div>

Yeah, I’d vote for using the environment variable to point at a JSON file containing the info (and possibly there’s alignment here with [PEP 739](https://discuss.python.org/t/pep-739-static-description-file-for-python-installations/44572)?).

I would also very much like a way to override the platform tag that `packaging` chooses for the current system. I’d love to be able to embed a configuration file in a Python environment that says “instead of `win32`, pretend this is `my_special_platform`” (and then point at a private package index to get perfectly matched custom builds). If you’re going to go for a big change here, please keep going 😃

---

<div class="post-metadata">

**Author:** ![FFY00](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ffy00/32/4007_2.png) [@FFY00](https://discuss.python.org/u/FFY00)\
**Post date:** [January 30, 2024, 8:27pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/6 "2024-01-30T20:27:18Z")

</div>

> [@steve.dower](#):
>
> Yeah, I’d vote for using the environment variable to point at a JSON file containing the info (and possibly there’s alignment here with [PEP 739](https://discuss.python.org/t/pep-739-static-description-file-for-python-installations/44572)?).

Yeah, I agree with Steve.

Also, If someone wants to write a PEP for this, I am open to working together.

---

<div class="post-metadata">

**Author:** ![ofek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ofek/32/1033_2.png) [@ofek](https://discuss.python.org/u/ofek)\
**Post date:** [January 30, 2024, 8:40pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/7 "2024-01-30T20:40:56Z")

</div>

> [@FFY00](#):
>
> > [@steve.dower](#):
> >
> > Yeah, I’d vote for using the environment variable to point at a JSON file containing the info (and possibly there’s alignment here with [PEP 739](https://discuss.python.org/t/pep-739-static-description-file-for-python-installations/44572)?).
> 
> Yeah, I agree with Steve.

Yes this is a much better idea and I would also prefer it!

> [@pf\_moore](#):
>
> Would you instead be willing to provide a fix for [Various package-index filtering flags do not affect the environment markers · Issue #11664 · pypa/pip · GitHub](https://github.com/pypa/pip/issues/11664)? It seems like that would be less controversial, and from what you say would also address your issue.

I don’t have time for that and I basically know nothing about the pip code base. Also, I would like more feedback here before considering this controversial 😉

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [January 30, 2024, 8:42pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/8 "2024-01-30T20:42:43Z")

</div>

> [@ofek](#):
>
> Would folks here generally be open to changes in [packaging/src/packaging/markers.py at 4d8534061364e3cbfee582192ab81a095ec2db51 · pypa/packaging · GitHub](https://github.com/pypa/packaging/blob/4d8534061364e3cbfee582192ab81a095ec2db51/src/packaging/markers.py#L175) such that an environment variable of JSON could determine the default environment?

As a maintainer of `packaging` I don’t love the idea. This feels like a hack to work around other tools that don’t let you specify the environment details explicitly. That function (which we are working on documenting) just gives you the running interpreter’s details, not the way to get arbitrary details of another interpreter.

> [@ofek](#):
>
> I think, however, even if that bug is fixed it would be useful to have a centralized place to explicitly define the environment since `packaging` is used by everything.

I’m not quite seeing why `packaging` is the expected coordination point of passing around data. Isn’t that what function arguments or instance attributes are for in your code? 😉

---

<div class="post-metadata">

**Author:** ![ofek](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ofek/32/1033_2.png) [@ofek](https://discuss.python.org/u/ofek)\
**Post date:** [January 30, 2024, 8:43pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/9 "2024-01-30T20:43:36Z")

</div>

> [@pf\_moore](#):
>
> Having said this, I _am_ in favour of having an API in `packaging` that can be used to configure the default environment, so that there’s a way for applications like pip to change it _without_ needing to pass an explicit environment to every call that evaluates a marker.

@brettcannon Are you in favor of this?

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [January 30, 2024, 8:53pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/10 "2024-01-30T20:53:31Z")

</div>

> [@ofek](#):
>
> > [@pf\_moore](#):
> >
> > Having said this, I _am_ in favour of having an API in `packaging` that can be used to configure the default environment, so that there’s a way for applications like pip to change it _without_ needing to pass an explicit environment to every call that evaluates a marker.
> 
> @brettcannon Are you in favor of this?

Not really because now we are just a data store at some module scope that anyone could have done themselves. Toss in that we would then have to manage threaded access, etc. and it seems like an increase in scope for the package (I don’t think we have persistent state anywhere right now, even when people complain about performance and ask for it to work around pip).

But I also like functional programming, so passing state around doesn’t bother me.

You can also see if @pradyunsg disagrees and/or try to sneak it in while I’m out in April. 😉

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 30, 2024, 9:07pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/11 "2024-01-30T21:07:39Z")

</div>

> [@pf\_moore](#):
>
> Windows doesn’t have the “temporary environment variable” feature from Unix of `ENV_VAR=xxx cmd`,

Fyi windows cmd does its done like this:

```python
setlocal
set ENV_VAR=xxx
command
endlocal

```

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [January 30, 2024, 9:09pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/12 "2024-01-30T21:09:48Z")

</div>

> [@brettcannon](#):
>
> Not really because now we are just a data store at some module scope that anyone could have done themselves. Toss in that we would then have to manage threaded access, etc. and it seems like an increase in scope for the package

This is why an environment variable or config file into `packaging` makes more sense than pushing it out to every tool - you only need to pick up the user-provided setting once, and it becomes the global constant, rather than the code it currently has to calculate the value from ambient information.

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [January 30, 2024, 9:10pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/13 "2024-01-30T21:10:23Z")

</div>

> [@barry-scott](#):
>
> Fyi windows cmd does its done like this:

Does that work on the command line or only in batch files (I thought it was the latter)? Although it’s only of mild interest as I use Powershell.

Sorry, I know this is offtopic.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [January 30, 2024, 9:13pm UTC](https://discuss.python.org/t/allow-for-overriding-the-default-environment/44581/14 "2024-01-30T21:13:44Z")

</div>

> [@pf\_moore](#):
>
> Does that work on the command line or only in batch files (I thought it was the latter)?

Not tried as an interactive commands. Cmd.exe is hard to use outside of batch files… To really see scary stuff have a read of `for/?` to see what the enterprising nerd can do in cmd.
