简体   繁体   English

从命令行运行Python脚本时,MySQLdb模块可以工作,但不能作为cron作业

[英]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.

相关问题 Python-运行cron作业时出现“ IOError:[Errno 13]权限被拒绝”,但从命令行运行时却没有 - Python - “IOError: [Errno 13] Permission denied” when running cron job but not when running from command line Expect 脚本在命令行中运行良好,但在 cron 作业中不起作用 - Expect script works fine in command line but does not work in cron job Python代码不适用于cron作业,但可以通过virtualenv从命令行使用 - Python code not working from cron job but works from command line via virtualenv Python脚本在命令行中有效,但在运行.py文件时无效 - Python script works in command line, but not when running the .py file Python 串行模块在命令行中工作,但不能从脚本中工作 - Python serial module works in command line but not from script 当使用 cron 管理器时,脚本在从 cron 作业和使用 django manage.py 的命令行运行时表现不同 - Script behaves differently when run from cron job and from command line using django manage.py when using a cron supervisor python脚本可从命令行运行,但无法从Web应用程序执行时运行 - python script works from command line but not when it is executed from webapplication python中的谷歌电子表格阅读脚本不是从cron计划运行而是在我运行shell命令时工作 - google spreadsheet reading script in python not running from cron schedule but working when i am running it shell command 当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗? - Python script works when I run from IDLE but not in command line? 从命令提示符运行python脚本时没有模块错误 - No module error when running python script from command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM