Python cannot find shared library in cron

2024/10/10 14:27:18

My Python script runs well in the shell. However when I cron it (under my own account) it gives me the following error:

/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

The first line of the script has:

#!/usr/local/bin/python

I know I have the following line in my ~/.bashrc file, which explains it works in the shell

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

If I cron it using the following it also works, but it looks ugly, and I hate to apply to every cron job.

00 * * * 1-5    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib && /path/somejob.py 

Is there a better way to do it? I know our admin used to have an earlier version of Python installed on some shared nfs path, and it does not require any system level config change as mentioned here. Our old Python script simply has this line as the first line no explicit setting the LD_LIBRARY_PATH.

#!/nfs/apps/python/bin/python

In the old nfs installation

/nfs/apps/python/-- bin-- lib-- share-- include

Current Python is 2.7.3, and it is installed as follows: (Linux CentOS 6)

./configure --prefix=/usr/local --enable-shared --with-system-expat --with-system-ffi
make
make install

Update:

  1. As ansh0I suggested, adding LD_LIBRARY_PATH to the top of cronab works!

  2. The reason python complained about the shared libraries is it is installed with --enable-shared. As a result the python binary file is much smaller, with much of the real interpreter code shared in /usr/local/lib/libpython2.7.so. Then you need to tell python where to find the shared library by setting LD_LIBRARY_PATH. If python is installed without --enable-shared, the binary file itself is much larger, and you don't need to specify any LD_LIBRARY_PATH

Answer

Assuming your LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib expression is working fine, you can set up environment variables at the top of the crontab file like below

#Setting up Environment variables
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib#Here follow the cron jobs
* * * * *   echo $LD_LIBRARY_PATH >> /home/user/logfile.log
* * * * *   some/cron/job.py
https://en.xdnf.cn/q/69882.html

Related Q&A

Multiple async unit tests fail, but running them one by one will pass

I have two unit tests, if I run them one by one, they pass. If I run them at class level, one pass and the other one fails at response = await ac.post( with the error message: RuntimeError: Event loop…

Pyusb on Windows 7 cannot find any devices

So I installed Pyusb 1.0.0-alpha-1 Under Windows, I cannot get any handles to usb devices.>>> import usb.core >>> print usb.core.find() NoneI do have 1 usb device plugged in(idVendor=…

How to speed up nested for loops in Python

I have the following Python 2.7 code:listOfLists = [] for l1_index, l1 in enumerate(L1):list = []for l2 in L2:for l3_index,l3 in enumerate(L3):if (L4[l2-1] == l3):value = L5[l2-1] * l1[l3_index]list.ap…

Folium Search Plugin No Results for FeatureGroup

Im trying to add search functionality to a map Im generating in Python with Folium. I see there is a handy Search plugin available and able to implement it successfully and get it added to the map. Unf…

writing dictionary of dictionaries to .csv file in a particular format

I am generating a dictionary out of multiple .csv files and it looks like this (example):dtDict = {AV-IM-1-13991730: {6/1/2014 0:10: 0.96,6/1/2014 0:15: 0.92,6/1/2014 0:20: 0.97},AV-IM-1-13991731: {6/1…

How to import SSL certificates for Firefox with Selenium [in Python]?

Trying to find a way to install a particular SSL certificate in Firefox with Selenium, using the Python WebDriver and FirefoxProfile. We need to use our own, custom certificate which is stored in the …

Cell assignment of a 2-dimensional Matrix in Python, without numpy

Below is my script, which basically creates a zero matrix of 12x8 filled with 0. Then I want to fill it in, one by one. So lets say column 2 row 0 needs to be 5. How do I do that? The example below sh…

Fill matplotlib subplots by column, not row

By default, matplotlib subplots are filled by row, not by column. To clarify, the commandsplt.subplot(nrows=3, ncols=2, idx=2) plt.subplot(nrows=3, ncols=2, idx=3)first plot into the upper right plot o…

Find the 2nd highest element

In a given array how to find the 2nd, 3rd, 4th, or 5th values? Also if we use themax() function in python what is the order of complexity i.e, associated with this function max()?.def nth_largest(…

pandas data frame - select rows and clear memory?

I have a large pandas dataframe (size = 3 GB):x = read.table(big_table.txt, sep=\t, header=0, index_col=0)Because Im working under memory constraints, I subset the dataframe:rows = calculate_rows() # a…