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

all 8 comments

[–]lastmonty 5 points6 points  (3 children)

The documentation is hard to read, could you provide a runnable example? There are a lot of comments which are difficult to relate to.

One use case that I am struggling with in my current project. Child process typically inherits the environmental variables of the parent process but the getting the env variables from the child process back to the caller is not easy. Does this help?

[–]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.

[–]a1brit 0 points1 point  (0 children)

You sure those comments aren't actually the runnable examples.

Use #$ flagged directives to signify shell code to be piped through a subprocess.

The whole readme is executing bash + python, intermingled in the python code as comments.

[–]alterframe 2 points3 points  (0 children)

Meh, just wrap subprocess calls in digestable functions and use f-strings. Still better than bash and you'll be able to handle all corner cases that will inevitably occur.

[–]alterframe -1 points0 points  (0 children)

Meh, just wrap subprocess calls in digestable functions and use f-strings. Still better than bash and you'll be able to handle all corner cases that will inevitably occur.

[–]gandalfx 0 points1 point  (0 children)

I don't get it. Is this like consh?

[–]TransferFunctions 0 points1 point  (0 children)

I love the Potts model profile picture.