0

Once a variable from os.environ or sys.path has been changed is there any way to load the original values for os.environ or sys.path that the rest of the operating system sees. That is to say without restarting python.

I have an embedded version of python 2.7 that has custom values but I need to load the default system values.

Gimp image editor has a version of python 2.7 built in for running scripts. I am trying to use a subprocess inside of gimp's 2.7 python to launch python 3.6 to run some code. I am trying to do this because I would like to make a plugin that uses tensorflow on windows. Using neuralsytle/faststyle inside of gimp with a plugin gui would be great.

The problem is that gimp loads up custom values for path and pythonpath for the python 2.7 that is built into it. Those values are inherited by the subprocess I use to launch python 3.6.

>>> sys.path
['', 'C:\\Projects\\caffe\\python', 'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\python36.zip', 'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\DLLs', 'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\lib', 'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36', 'C:\\Users\\audov\\AppData\\Roaming\\Python\\Python36\\site-packages', 'C:\\Users\\audov\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages']

Becomes

>>> sys.path
['', 'C:\\Program Files\\GIMP 2\\32\\lib\\gimp\\2.0\\python', 'C:\\Program Files\\GIMP 2\\lib\\gimp\\2.0\\plug-ins\\python-console', 'C:\\Program Files\\GIMP 2\\32\\lib\\python27.zip', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\plat-win32', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\lib-tk', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\lib-old', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\lib-dynload', 'C:\\building\\msys64\\mingw32', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\site-packages', 'C:\\Program Files\\GIMP 2\\32\\lib\\python2.7\\site-packages\\gtk-2.0']

How would I load up the standard values for those environment variables that the rest of the operating system sees.

import os, sys
os.environ
sys.path

Will display the original values for variable that have nothing to do with python. And then modified values for python and gimp.

Is there a way to load the unmodified environment values in a generic way.

I am running Windows 10.

I know I can append to the path or replace the path variable with values I copy beforehand. I could also have a string that I use to set env=foo on the subprocess.

But that is not a very good solution. I would like this to potentially work on other systems.

This is a hard question to look up the solution to because it is confused in search terms for finding out how to set the environment variables or read them normally.

EDIT Start new subprocess with 'default' environment variables

This question runs into the same problem and has a partial solution. As they put it "I'm not looking to pass an environment variable to a subprocess; I'm looking for the subprocess to return a default environment."

Unfortunately it is still not enough to get tensorflow to run in the new process. I will keep trying.

Audo Voice
  • 43
  • 5
  • How are you launching Python 3 from within your Python 2 Gimp plugin? It would be very useful to also include that code in your question. – metatoaster Dec 06 '18 at 00:11
  • The only way it can affect `sys.path` in a child process is if it sets the `PYTHONPATH` environment variable. Note that this has nothing at all to do with `PATH`, which has nothing to do with `sys.path`. It may be changing `PATH` as well, but you don't mention any problems related to that. Anyway, if your embedded 2.7 has the subprocess module, use `subprocess.call` and pass a modified copy of the `os.environ` dict as its `env` parameter. – Eryk Sun Dec 06 '18 at 00:14
  • I know I can pass it a modified copy of the os.environ as a dict but where would I get that from within 2.7. If I just saved a copy of os.environ from running it normally from command line then it would just be a copy of MY environment. It would not be a way to do this entirely from within gimp. Within a version of python with a modified environment variable how would it read the normal default environment variable. I apologize for not being clear. – Audo Voice Dec 06 '18 at 00:42
  • You can modify your saved copy of `os.environ` to remove `PYTHONPATH` or any other unwanted environment variables, from within the Gimp environment before passing that to `subprocess.call` through the `env` parameter. – metatoaster Dec 06 '18 at 00:48
  • Ok I will try to use unsetenv() to remove path an pythonpath from the env dict that I pass to the subprocess. It might default to some value that works. But I want to set them to what they are for the rest of the system. I need the environment path values that programs normally see. I suspect if I pass an environment with no path value defined it will just have path be undefined. I will try it now. – Audo Voice Dec 06 '18 at 01:05
  • os.environ.clear() sets environ to be be nothing. If I then do subprocess.call(["C:/Users/audov/AppData/Local/Programs/Python/Python36/python.exe"]) Python starts but then crashes and gives an error because it has no environment variables set. So it does not try to reload them when they are not passed in. It was worth a try. – Audo Voice Dec 06 '18 at 01:15
  • With the PyWin32 extensions installed, you can load the default environment for the current user with the following snippet of code: `environ = win32profile.CreateEnvironmentBlock(win32security.OpenProcessToken(win32process.GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE), False)`. – Eryk Sun Dec 06 '18 at 07:02

0 Answers0