contactprad
contactprad

Reputation: 11

How to export DYLD_LIBRARY_PATH on mac using python script

I am using ctypes to load my library. Code is something like

currPath = os.getcwd() 
libPath = os.path.join(currPath, platform) 
print libPath 
if(platform == 'win32'): 
    os.environ['PATH'] = os.environ['PATH'] + r';' + libPath 
if(platform == 'darwin'): 
    os.environ['DYLD_LIBRARY_PATH'] = libPath {/Users/labuser/Desktop/lib} 
print os.environ['DYLD_LIBRARY_PATH']

But when I try to load my library, it doesn't find the library path.

What are the probable solution. When on terminal before launching the python script I do export DYLD_LIBRARY_PATH = PATH, then my python script works fine.

Upvotes: 1

Views: 3141

Answers (1)

jdi
jdi

Reputation: 92647

If you are trying to set DYLD_LIBRARY_PATH in the same script that loads your library code, I think this will not work. The environment variable needs to be set before python starts up, from the shell. Setting it in os.environ is not persistant beyond the current python environment anyways.

What @summea refers to in his comment link is the need to create some type of shell script, or adding this to your shell environment via ~/.profile or whatever is the equivalent location for your operating system and shell.

The reason that it works, when you export the variable and then start your script is that the variable is set when python starts. Equally you could probably launch your script with something like DYLD_LIBRARY_PATH="path" ./script.py and it would also work with the temporary variable setting.

Upvotes: 4

Related Questions