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:
As ansh0I suggested, adding LD_LIBRARY_PATH to the top of cronab works!
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
--enable-shared
just saved me – little-dude