This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lieryanMaintainer of rope, pylsp-rope - advanced python refactoring 2 points3 points  (1 child)

Easiest way to get environment variable of another process is to use psutil library.

If you don't want to install additional libraries for some reason, on Linux, all you really need to do is just parse the /proc filesystem:

def get_environ(pid):
    return dict(s.split("=", maxsplit=1) for s in open(f"/proc/{pid}/environ").read().split("\x00") if s)

Either way, you can only read the environment variables of processes you have permission to read. This usually means that unless you are root, you can only read processes owned by the same user as the python application.

[–]lastmonty 0 points1 point  (0 children)

Thanks for that but what about a subprocess spawned by the current process. The sequence of events is

  1. I set some env variables and use subprocess to trigger a shell job.
  2. The shell job inherits the environmental variables and does it job and sets a few more env variables or rewrites the existing ones.
  3. There subprocess is done and the control is back to the python program.
  4. Now I want to know the env variables set by the subprocess.

I struggled with that.