且构网

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

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

更新时间:2023-11-18 18:40:28

可能是您使用了不同的 Python 可执行文件.在 shell 上,输入 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

也可能是您的 shell 具有名为 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/')