Forums

Unable to load libraries despite setting LD_LIBRARY_PATH in WSGI file

Hi,

I installed the cf_units module from scratch. This created a libudunits2.so.0 file stored in /home/samhat/udunits2/lib. In my app directory, my .env file looks like

LD_LIBRARY_PATH=/home/samhat/udunits2/lib

and in my /var/www/samhat_pythonanywhere_com_wsgi.py file I have

from dotenv import load_dotenv

project_folder = os.path.expanduser('/home/samhat/app-name')

load_dotenv(os.path.join(project_folder, '.env'))

I am able to view the LD_LIBRARY_PATH from within the web app, using os.environ. However, when I try to import cf_units, I get

ImportError: libudunits2.so.0: cannot open shared object file: No such file or directory

despite the fact that this file definitely exists in the /home/samhat/udunits2/lib directory. I am able to import the cf_units module from a bash command prompt by manually setting LD_LIBRARY_PATH, but it doesn't work for the web app itself. Any ideas why the file cannot be resolved despite existing in the linking library path? Thanks.

Hmm. I believe that LD_LIBRARY_PATH is only checked when a program starts up, and changing it at runtime doesn't have any effect. So that explains why it's not working. Unfortunately, I can't think of any solution to that directly.

One thing that might work is if you put the .so file(s) in ~/.local/lib which should already be on the library path -- maybe give that a go and let us know how you get on?

Thanks for the suggestion. Unfortunately that didn't work either.

The cf_units module is a dependency for Iris which is a fantastic library for processing meteorological data. For now I'll try using the netcdf4 module, though I suspect I'll have the same issue, as that depends on cftime which is Cython-based, much like cf_units.

The netCDF4 module works great! Please do consider some way to support Conda in the future, though, as there are several important packages for analysing geospatial data which are not so easy to install without it.

Excellent! That's good news. I've upvoted the "support Conda" ticket; it does work in consoles and scheduled tasks with a bit of setup, but (as perhaps you already know) not in website code.

Hello,

I have the same issue where the .so file is found when running in bash console but the web app itself will not find the file. Is there a way to restart the web app and run it with the same environmental variables as the bash console including LD_LIBRARY_PATH?

Thank you!

LD_LIBRARY_PATH only has an effect before the program is loaded. It does not work in web apps because doing it in the wsgi file is too late and there is no way to do it earlier. You can offload the code that needs the LD_LIBRARY_PATH to be set by running the code that requires it in a scheduled task or an always on task by prefixing the command with LD_LIBRARY_PATH=/path. Then you can have the web app send jobs to that process: https://help.pythonanywhere.com/pages/AsyncInWebApps/