Skip to content

Mirror Elixir env vars into os.environ on initialization#43

Closed
jonatanklosko wants to merge 4 commits intomainfrom
jk-env
Closed

Mirror Elixir env vars into os.environ on initialization#43
jonatanklosko wants to merge 4 commits intomainfrom
jk-env

Conversation

@jonatanklosko
Copy link
Member

Closes #42.

Comment on lines +59 to +60
> Also note that contrarily to Elixir, changes to `os.environ` are
> automatically mirrored to the OS process environment.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a consequence, if we do System.put_env("FOO", "bar"), and then we call Pythonx.uv_init(...), this will set env var "FOO" in the OS process environ. I don't think it's an issue though. @josevalim any objections?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can argue both ways: copy the Elixir env vars or keep it as is. However, given that setting the Python env vars also affect the OS ones, then there is a larger side-effect here is that initing Python changes the OS env vars, which I am not sure we should do. So I actually think we should document Python only inherits the process env vars and any other need to be copied explicitly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair enough.

Comment on lines 394 to 398
auto py_os_environ_clear = PyObject_GetAttrString(py_os_environ, "clear");
raise_if_failed(env, py_os_environ_clear);
auto py_os_environ_clear_guard = PyDecRefGuard(py_os_environ_clear);
auto result = PyObject_CallNoArgs(py_os_environ_clear);
raise_if_failed(env, result);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah, we cannot call os.environ.clear() and just set everything from scratch, because Windows has env var keys like =D:, which we can read, but we cannot set 😂 So we need to diff both ways.

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

Successfully merging this pull request may close these issues.

Pass environment variables to the python interpreter

2 participants