我一整天都在努力弄清楚如何正确安装python模块,以便crontab可以访问它。下面我概述了我正在采取的步骤,并希望有人告诉我我在哪里犯了错误以及我如何改进。
在我到达第8步之前,一切看起来都很好
绝对欢迎所有反馈
第1步 - 检查beatbox
>>> import beatbox
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named beatbox
第2步 - 安装Beatbox
MacBook-Pro-10:~ $ sudo pip install beatboxxx
Downloading/unpacking beatboxxx
Downloading beatboxxx-21.5.tar.gz
Running setup.py (path:/private/tmp/pip_build_root/beatboxxx/setup.py) egg_info for package beatboxxx
warning: no files found matching '*.txt' under directory 'examples'
no previously-included directories found matching 'src/beatbox/tests/sfconfig.py'
Installing collected packages: beatboxxx
Running setup.py install for beatboxxx
warning: no files found matching '*.txt' under directory 'examples'
no previously-included directories found matching 'src/beatbox/tests/sfconfig.py'
Successfully installed beatboxxx
Cleaning up...
第3步 - 验证beatbox位置
>>> import beatbox
>>> print beatbox.__file__
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/beatbox/__init__.pyc
步骤4 - 检查目录是否在sys.path
中>>> import sys
>>> for i in sys.path:
... print i
...
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Python/2.7/site-packages
步骤5 - 将Beatbox添加到sys.path
>>> import sys
>>> sys.path.append('/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/beatbox')
>>> x = '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/beatbox'
>>> x in sys.path
True
>>>
第6步 - 创建导入beatbox的python文件 MacBook-Pro-10:桌面$ vim beatbox_test.py
import beatbox
~
~
第7步 - 创建crontab MacBook-Pro-10:桌面$ crontab -e
*/1 * * * * python /Users/user123121231/Desktop/beatbox_test.py
crontab: installing new crontab
步骤8 - 检查邮件crontab成功
MacBook-Pro-10:Desktop $ mail
"/var/mail/Message 1: 1 message 1 new
>N 1 MacBook-P Sun Oct 26 11:11 22/889 "Cron <MacBook-Pro-10> python /Users/user123121231/Desktop/beatbox_test.py"
From User123142324@User123142324s-MacBook-Pro-10.local Sun Oct 26 11:11:01 2014
X-Original-To: User123142324
Delivered-To: User123142324@User123142324s-MacBook-Pro-10.local
From: User123142324@User123142324s-MacBook-Pro-10.local (Cron Daemon)
To: User123142324@User123142324s-MacBook-Pro-10.local
Subject: Cron <User123142324@User123142324s-MacBook-Pro-10> python /Users/User123142324/Desktop/beatbox_test.py
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=User123142324>
X-Cron-Env: <USER=User123142324>
X-Cron-Env: <HOME=/Users/User123142324>
Date: Sun, 26 Oct 2014 11:10:00 -0700 (PDT)
Traceback (most recent call last):
File "/Users/User123142324/Desktop/beatbox_test.py", line 1, in <module>
import beatbox
ImportError: No module named beatbox":
更新:仍然没有解决方案,但我发现了一些东西。我将beatbox_test.py修改为以下内容:
import sys
for i in sys.path:
print i
import beatbox
返回以下内容:
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload
/Library/Python/2.6/site-packages
为什么一切都指向2.6而不是2.7?
答案 0 :(得分:1)
在crontab中使用此行指向特定的Python版本(以及为其安装的所有软件包):
*/1 * * * * /usr/bin/python2.7 /Users/user123121231/Desktop/beatbox_test.py
答案 1 :(得分:0)
您可能需要在crontab中指定相应python版本的完整路径,否则可能会运行错误的版本并在错误的位置查找已安装的模块。假设从命令行启动程序时程序运行正常,可以使用which命令查找正在使用的python可执行文件的位置。
$ which python
/usr/bin/python2.7
然后在crontab条目中使用此路径指向特定的Python版本(以及为其安装的所有软件包):
*/1 * * * * /usr/bin/python2.7 /Users/user123121231/Desktop/beatbox_test.py