4

In iPython, I import a module, which in turn imports another module. This another module (namely gurobipy) tries to load its shared library, which fails. The path to the shared library is added to LD_LIBRARY_PATH in .bashrc. When I run the whole thing as a script from the shell, everything is fine.

I googled for a while now but didn't find the ultimate answer to the question: how do I set LD_LIBRARY_PATH in iPython such that imported modules will see it?

I tried os.environ but still got the same error message: ImportError: libgurobi50.so: cannot open shared object file: No such file or directory

EDIT: I'm using Ubuntu 13.04.

Konstantin
  • 2,451
  • 1
  • 24
  • 26
  • How are you starting IPython? If you start it from a terminal, it should get the same environment variables. You should be able to modify environment variables using [os.environ](https://docs.python.org/3/library/os.html#os.environ). – Thomas K Sep 23 '14 at 16:30
  • Yes, I forgot to mention that. I'm starting from the Unity launcher. You're right, when started from a terminal, it works. I am able to modify LD_LIBRARY_PATH with os.environ. However, when I then import the module, it still does not find the library. – Konstantin Sep 24 '14 at 07:27
  • Possibly LD_LIBRARY_PATH has to be set before the process starts. – Thomas K Sep 24 '14 at 17:02

2 Answers2

2

Try including LD_LIBRARY_PATH to a system-wide file. For instance, /etc/profile (at the end of it). For instance this is what I needed to add for my case:

export NEVESIM_HOME=/home/kam/Applications/Nevesim

export LD_LIBRARY_PATH=${NEVESIM_HOME}/lib:${LD_LIBRARY_PATH}

Be careful with the syntax. Also you have to re-log in to make the changes active. For more help see https://help.ubuntu.com/community/EnvironmentVariables.

Tsubaki Hana
  • 33
  • 1
  • 4
  • 1
    I was thinking about that as well. I would, however, be really curious to know if it can be done from within the python script itself or from within an iPython shell. – Konstantin Sep 25 '14 at 07:50
2

The runtime library path is configured when the python (or IPython) interpreter starts up. Read this.

It seems that tools like ctypes use this path when searching for libraries. Likewise, if a module depends on a particular library, it will search this path when it's imported or run.

This is true for any process, not just IPython. You can't just change the loader path while a process is running. Also read this.

If you think about it, that's really a good thing. It could cause all kinds of problems. All of the sudden, the process can't find a library it needs, and crashes.

This is unfortunate, though, as it means that IPython can't really be used as a full shell replacement.

I'm not sure how shells such as BASH handle this. I imagine that BASH uses one runtime path for itself, and exports another for processes run inside it.

Community
  • 1
  • 1
orodbhen
  • 2,644
  • 3
  • 20
  • 29