Cannot import mysql-python in crontab Mac OS

戏子无情 提交于 2019-12-07 20:48:46

问题


I wrote a script which runs ok, but when I run it from crontab I get an import error.

   Traceback (most recent call last):
  File "/Users/.../Desktop/test.py", line 3, in <module>
    import MySQLdb as mysql
  File "build/bdist.macosx-10.7-intel/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.macosx-10.7-intel/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.7-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/.../.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/.../.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
  Reason: image not found

I tried adding this #!/usr/bin/python to the top of my script but the problem is still there.

I am working on Mac OS version 10.7

Edit: Crontab try to load mysql-python from current directory (which is my Home folder) lopen(/Users/.../.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2), while the library locate in /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg, i tried to use PYTHONPATH=/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 in crontab but it's not working, any suggestion?

Thanks.


回答1:


I solved this by using the following solution from : https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables

In my case it was because Cron didn't have access to my Environment variables that point to the correct place the Mysql library.

I used:

8 10 * * * . $HOME/.bash_profile; /Path/To/Script/pythonDbScript.py



回答2:


I think you should check for your python version. On mac, it maybe installed several version of python. e.g. 2.5, 2.6, 2.7

When you run on terminal, you may using python2.6, but for the cron job, it my use the 2.7

use python -V to see which version of python are you using.

use which python to locate the python path

use shell autocomplete to see how many pythons did you install when you type python, then press "tab", you will find the result.




回答3:


From the Mysqldb faq: http://mysql-python.sourceforge.net/FAQ.html

Another thing that can cause this: The MySQL libraries may not be on your system path.

Solutions:

set the LD_LIBRARY_PATH environment variable so that it includes the path to the MySQL libraries.

set static=True in site.cfg for static linking

This probably means that in your crontab (remember minimal shell environment) the mysql libraries are not accessible (not on the path, not in the LD_LIBRARY_PATH)

check your environment variables between the 'regular' shell and the 'crontab' shell




回答4:


try below:

sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib


来源:https://stackoverflow.com/questions/8339788/cannot-import-mysql-python-in-crontab-mac-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!