且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用crontab运行脚本时无法导入Python MySQL模块

更新时间:2023-11-18 18:36:34

可能是您正在使用其他Python可执行文件.在外壳程序上,输入which python以找出Python可执行文件所在的位置.假设这返回了/usr/bin/python以外的其他内容,例如/home/myuser/bin/python,那么在脚本的第一行中,您将编写:

It may be that you're using a different Python executable. On the shell, enter which python to find out where the Python executable is located. Let's say this returns something other than /usr/bin/python, say /home/myuser/bin/python, then in the first line of your script, you would write:

#!/home/myuser/bin/python

也可能是您的外壳程序具有名为PYTHONPATH的环境变量.如果是这种情况,并且您发现它是从哪里导入库的,那么您将在脚本的第一行中,在 导入"MySQLdb"之前,添加查找该库的路径:

It may also be that your shell has environment variable called PYTHONPATH. If that's the case and you find where it's importing the library from, then this is how you would add the path to find the library in the first line of your script, before the import of "MySQLdb":

import sys; sys.path.append('/path/to/MySQLdb-lib/')