Search code examples
pythonmacosshebang

How to use `env python3` and retaining the calling environment?


I have a problem on OSX that #!/usr/bin/env python3 is causing the environment variables, such as library paths (LD_LIBRARY_PATH for example), to be lost. On Linux it works correctly.

Reading the man page on env it doesn't appear it should be doing this. It should only be modifying the environment if I request it, and I am not.

What is a portable shebang line that preserves the environment?

Note: I'm detecting the problem since a call to subprocess.open on one of my programs fails since it can't find one of the libraries. Yet if I start python interactively the same call works fine.


Solution

  • This is the OS X System Integrity Protection at work; certain 'dangerous' environment variables such as DYLD_LIBRARY_PATH, and, as you discovered, LD_LIBRARY_PATH, are not passed to any binary with a /usr/bin or /bin path.

    As such, /usr/bin/env never sees any of several environment variables and can't pass those on to the python3 child process it spawns.

    See several other posts on the subject:

    If you are using subprocess, I'd check for the shebang line, extract the binary named after env, prefix your command with that binary, and bypass env altogether.