[英]MySQLdb module works when running Python script from the command line, but not as a cron job
I am trying to create a cron job to run a python script I wrote, but for some reason the script only runs when I'm running it from the command line. 我试图创建一个cron作业来运行我编写的python脚本,但是由于某种原因,该脚本仅在从命令行运行时才运行。 It worked before, but I accidentally deleted my crontab.
以前可以使用,但是我不小心删除了crontab。 When the crontab I just wrote tries to run it, I get an error about the MySQLdb module being the wrong architecture:
当我刚刚编写的crontab尝试运行它时,我收到有关MySQLdb模块结构错误的错误:
Traceback (most recent call last):
File "/Users/myuser/Documents/EE_SFDC Integration/csv_reader/Weekly_Updates/Weekly_Confirmation/weekly_confirm.py", line 7, in <module>
import MySQLdb
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): no suitable image found. Did find:
/Library/Python/2.7/site-packages/_mysql.so: mach-o, but wrong architecture
In my crontab, I've tried using the full path to several python versions (2.7 and 3.4), but I still get the issue. 在我的crontab中,我尝试使用多个python版本(2.7和3.4)的完整路径,但仍然遇到问题。 When I use 3.4, I don't get an error about MySQLdb, but I do get an error on a print .forma() statement being the wrong syntax.
当我使用3.4,我没有得到有关MySQLdb的一个错误,但我得到的打印.forma()语句是错误的语法错误。
But again, that doesn't happen when I run the script from the command line, so I expect this is not the correct version either. 但是同样,当我从命令行运行脚本时也不会发生这种情况,因此我希望这也不是正确的版本。
The cron job looks like this: Cron作业如下所示:
14 12 * * * today=`date +"\%Y-\%m-\%d"` && /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2 /Users/myuser/Documents/EE_SFDC\ Integration/csv_reader/Weekly_Updates/Weekly_Confirmation/weekly_confirm.py > /Users/myuser/Documents/EE/run_logs/weekly_sync_runlog_$today.txt 2>&1
Any help is much appreciated! 任何帮助深表感谢!
The error message typically means that you have a mismatch between the MySQLdb (or a dependency) and Python architecture. 该错误消息通常表示您在MySQLdb(或依赖项)和Python体系结构之间不匹配。
That is, one of them are most likely 64-bit, while the other is 32-bit. 也就是说,其中一个很可能是64位,而另一个是32位。
What happens if you try the following in a terminal? 如果您在终端机中尝试以下操作会怎样?
$ /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2
(Take note of the exact version)
>>> import platform
>>> print platform.architecture()
>>> import MySQLdb
Do it again, but this time with python from your path and see if they differ 再做一次,但是这次从您的路径开始使用python,看看它们是否不同
$ python
>>> import platform
>>> print platform.architecture()
>>> import MySQLdb
Edit: 编辑:
You might also check with file, what architecture the module is built for. 您还可以使用文件检查该模块是针对哪种体系结构构建的。
$ file /Library/Python/2.7/site-packages/_mysql.so
/Library/Python/2.7/site-packages/_mysql.so: Mach-O universal binary with 2 architectures
/Library/Python/2.7/site-packages/_mysql.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
/Library/Python/2.7/site-packages/_mysql.so (for architecture i386): Mach-O bundle i386
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.