Set LD_LIBRARY_PATH before importing in python
UPDATE: see the EDIT below.
I would use:
import os
os.environ['LD_LIBRARY_PATH'] = os.getcwd() # or whatever path you want
This sets the LD_LIBRARY_PATH
environment variable for the duration/lifetime of the execution of the current process only.
EDIT: it looks like this needs to be set before starting Python: Changing LD_LIBRARY_PATH at runtime for ctypes
So I'd suggest going with a wrapper .sh
(or .py
if you insist) script. Also, as @chepner pointed out, you might want to consider installing your .so
files in a standard location (within the virtualenv).
See also Setting LD_LIBRARY_PATH from inside Python
Change current process environment's LD_LIBRARY_PATH
The reason
os.environ["LD_LIBRARY_PATH"] = ...
doesn't work is simple: this environment variable controls behavior of the dynamic loader (ld-linux.so.2
on Linux, ld.so.1
on Solaris), but the loader only looks at LD_LIBRARY_PATH
once at process startup. Changing the value of LD_LIBRARY_PATH
in the current process after that point has no effect (just as the answer to this question says).
You do have some options:
A. If you know that you are going to need xyz.so
from /some/path
, and control the execution of python script from the start, then simply set LD_LIBRARY_PATH
to your liking (after checking that it is not already so set), and re-execute yourself. This is what Java
does.
B. You can import /some/path/xyz.so
via its absolute path before importing x.so
. When you then import x.so
, the loader will discover that it has already loaded xyz.so
, and will use the already loaded module instead of searching for it again.
C. If you build x.so
yourself, you can add -Wl,-rpath=/some/path
to its link line, and then importing x.so
will cause the loader to look for dependent modules in /some/path
.
Changing LD_LIBRARY_PATH at runtime for ctypes
By the time a program such as Python is running, the dynamic loader (ld.so.1 or something similar) has already read LD_LIBRARY_PATH and won't notice any changes thereafter. So, unless the Python software itself evaluates LD_LIBRARY_PATH and uses it to build the possible path name of the library for dlopen()
or an equivalent function to use, setting the variable in the script will have no effect.
Given that you say it doesn't work, it seems plausible to suppose that Python does not build and try all the possible library names; it probably relies on LD_LIBRARY_PATH alone.
How to set LD_LIBRARY_PATH for apache+wsgi website
Is the library required by a Python module extension you have compiled and installed? If it was, set LD_RUN_PATH
environment variable to the directory the library is in when compiling and installing that Python module. That way the location is embedded in the Python module extension itself and you don't need LD_LIBRARY_PATH
at run time.
The only other way to do it using environment variables is to set LD_LIBRARY_PATH
in the startup scripts for Apache so that when Apache is started it is set as you require. This means fiddling with system startup scripts so is not ideal or always practical.
One final way which may work but isn't really that great of an idea and may not even work, is to use:
LoadFile "/usr/local/lib/libmylib.so"
in the Apache configuration. This will force linking of the specific library into Apache at startup, but depending on how the library is being used, this may not work.
Conda set LD_LIBRARY_PATH for env only
You can set environment variables when an environment is activated by editing the activate.d/env_vars.sh
script. See here: https://conda.io/docs/user-guide/tasks/manage-environments.html#macos-and-linux
The key portions from that link are:
Locate the directory for the conda environment in your Terminal
window, such as/home/jsmith/anaconda3/envs/analytics
.Enter that directory and create these subdirectories and
files:cd /home/jsmith/anaconda3/envs/analytics
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.shEdit
./etc/conda/activate.d/env_vars.sh
as follows:#!/bin/sh
export MY_KEY='secret-key-value'
export MY_FILE=/path/to/my/file/Edit
./etc/conda/deactivate.d/env_vars.sh
as follows::#!/bin/sh
unset MY_KEY
unset MY_FILE
When you run
conda activate analytics
, the environment
variables MY_KEY and MY_FILE are set to the values you wrote into
the file. When you runconda deactivate
, those variables are
erased.
CFFI how to avoid manual setting of LD_LIBRARY_PATH
Oh, I get it)
extra_link_args=['-Wl,-rpath=./lib'],
Big thanks to everyone!
Related Topics
How to Convert Index of a Pandas Dataframe into a Column
How to Read a File in Reverse Order
Do Not Want the Images to Load and CSS to Render on Firefox in Selenium Webdriver - Python
Multiprocessing: Use Only the Physical Cores
Python: Interplay Between Lib/Site-Packages/Site.Py and Lib/Site.Py
How to Benchmark Part of Tensorflow Graph
Kill Python Interpeter in Linux from the Terminal
Python: When to Use Pty.Fork() Versus Os.Fork()
Using Pyinotify to Watch for File Creation, But Waiting for It to Be Completely Written to Disk
Find the Similarity Metric Between Two Strings
Generating Variable Names on Fly in Python
CSS Problems with Flask Web App
Dropping Root Permissions in Python
Asyncio in Corroutine Runtimeerror: No Running Event Loop