0

I need to pass environment variable value in script which in interpreted in embedded (in iOS app) Python interpreter. I know I can use setenv before Py_Initialize, but it would be available to all the 3rd-party libraries. Is there any possibility to set environment variable value for Python interpret only? Something like Py_SetEnvVar(char *name, char *value) to make it available in python scripts:

os.environ.get('MY_ENV_VAR')
4ntoine
  • 19,816
  • 21
  • 96
  • 220
  • Perhaps you can come up with some other key/value store which you can set externally and query from a script - even building that on top of a raw memory buffer shouldn't be hard. Though what the problem with using prefixed environment variables would be is unclear; if there are things running *in your process* which you mistrust, then your problem isn't with environment variables. – Chris Stratton Oct 06 '14 at 16:27
  • the question is not in trust but in overriding variable values. anyway if there is no possibility to set env variables for python i will not try to reinvent the wheel and just use `setenv` – 4ntoine Oct 06 '14 at 19:07
  • If it's not a trust issue, then setting environment variables just for python is as simple as prefixing the names, ie "JUST_FOR_PYTHON_FOO" – Chris Stratton Oct 06 '14 at 19:23

1 Answers1

2

Okay, you didn't seem to agree with me, but I'll leave it here just in case anyone with a similar problem finds it useful. You can downvote it then or whatever if you want, it's fair.

There is no Py_SetEnvVar or anything similar because an embedded interpreter does not have a custom environment. It runs in the same process, and so the environment is the same one, and so the variables are the same ones. You cannot have two isolated environments, or a subenvironment, in one process. If your Python code relies on environment variables, and it must be run in an embedded interpreter, those variables will be visible for the whole process. If you want the variables to be hidden from 3rd party libraries in the same process, do not use environment variables.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • thanks for full reply. though i can't see any reason for embedded Python interpreter not to have it's subenvironment i appreciate your help. I'm not Python C API expert but i can see `Py_SetPythonHome` method which seems to set PYTHON_HOME environment variable only so i can expect similar methods for setting another custom environment variables for interpreted scripts only – 4ntoine Oct 06 '14 at 11:44
  • You are right, that function behaves as if `PYTHONHOME` was set to its argument, but I don't think it actually sets the environment variable. Either way, both `::getenv("PYTHONHOME")` in C++ and `os.environ['PYTHONHOME']` in Python will produce the same result after starting the interpreter, as with any other variable. The environment, being an OS-provided feature, is always bounded to the whole process. – jdehesa Oct 06 '14 at 11:56
  • @jdehesa It's been many years since your reply, but I have a question. You say that dlls run in the same environment, yet when I try to detour a winapi function, which gets used by python.dll (i've checked the source), the redirect does not work. That hints me at the fact that python.dll is not running in the same environment. Any ideas? – z0rberg's Aug 24 '20 at 06:29
  • @z0rberg's I don't know how exactly WinAPI detours work, but I don't think it has much to do with environment variables (which is essentially what I am referring to by "environment" here). In [this question](https://stackoverflow.com/q/39111709) a user seems to be able to hook the Python interpreter using `withdll.exe`. – jdehesa Aug 24 '20 at 09:16
  • I'm not actually using the "detours", just the same technique. I haven't figured out how "withdll" works, but that's not the topic here anyway. Thanks for clearing it up. – z0rberg's Aug 24 '20 at 09:22