Home > Uncategorized > Setting LD_LIBRARY_PATH in Python

Setting LD_LIBRARY_PATH in Python

I have a python module entirely written in C++ with third party dependencies not in LD_LIBRARY_PATH nor ldconfig. Let’s say

/home/me/python/mymodule/mymodule.so
/home/me/python/mymodule/libdep1.so
/home/me/python/mymodule/libdep2.so

I want to use this module without writing a shell script to set LD_LIBRARY_PATH. Here is how to do:

import sys, os
mymodule_path = "/home/me/python/mymodule"
try:
    sys.path.append(mymodule_path)
    import mymodule
except ImportError:
   if sys.platform == 'win32':
       os.environ['PATH'] = mymodule_path
   elif sys.platform == 'darwin':
       os.environ['DYLD_LIBRARY_PATH'] = mymodule_path
   else:
       os.environ['LD_LIBRARY_PATH'] = mymodule_path
   args = [sys.executable]
   args.extend(sys.argv)
   os.execv(sys.executable, args)
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: