I'm trying to run a custom django command as a scheduled task on Heroku. I am able to execute the custom command locally via: python manage.py send_daily_email
. (note: I do NOT have any problems with the custom management command itself)
我正在尝试将自定义django命令作为Heroku上的计划任务运行。我可以通过以下方式在本地执行自定义命令:python manage.py send_daily_email。 (注意:我对自定义管理命令本身没有任何问题)
However, Heroku is giving me the following exception when trying to "Run" the task through Heroku Scheduler addon:
但是,当尝试通过Heroku Scheduler插件“运行”任务时,Heroku会给我以下异常:
Traceback (most recent call last):
File "bin/send_daily_visit_email.py", line 2, in <module>
from django.conf import settings
ImportError: No module named django.conf
I placed a python script in /bin/send_daily_email.py, and it is the following:
我在/bin/send_daily_email.py中放了一个python脚本,它是以下内容:
#! /usr/bin/python
from django.conf import settings
settings.configure()
from django.core import management
management.call_command('send_daily_email') #delegates off to custom command
Within Heroku, however, I am able to run heroku run bin/python
- launch the python shell - and successfully import settings
from django.conf
但是,在Heroku中,我可以运行heroku run bin / python - 启动python shell - 并成功从django.conf导入设置
I am pretty sure it has something to do with my PYTHON_PATH
or visibility to Django's SETTINGS_MODULE
, but I'm unsure how to resolve the issue. Could someone point me in the right direction? Is there an easier way to accomplish what I'm trying to do here?
我很确定它与我的PYTHON_PATH或Django的SETTINGS_MODULE的可见性有关,但我不确定如何解决这个问题。有人能指出我正确的方向吗?有没有更简单的方法来完成我在这里尝试做的事情?
Thank you so much for your tips and advice in advance! New to Heroku! :)
非常感谢您的提示和建议! Heroku新手! :)
EDIT:
Per Nix's comment, I made some adjustments, and did discover that specifying my exact python path, I did get past the Django setup.
Per Nix的评论,我做了一些调整,并且确实发现指定我的确切python路径,我确实通过了Django设置。
I now receive:
我现在收到:
File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 155, in call_command
raise CommandError("Unknown command: %r" % name)
django.core.management.base.CommandError: Unknown command: 'send_daily_email'
Although, I can see 'send_daily_email' when I run ``heroku run bin/python app/manage.py```.
虽然,当我运行``heroku run bin / python app / manage.py```时,我可以看到'send_daily_email'。
I'll keep an update if I come across the answer.
如果我遇到答案,我会保留更新。
2 个解决方案
#1
3
You are probably using a different interpreter.
您可能正在使用不同的解释器。
Check to make sure shell python is the same as the one you reference in your script /usr/bin/python . It could be that there is a different one in your path, which would explain why it works when you run python manage.py
but not your shell scrip which you explicitly reference /usr/bin/python
.
检查以确保shell python与脚本/ usr / bin / python中引用的shell相同。可能是你的路径中有一个不同的,这可以解释为什么它在你运行python manage.py而不是你明确引用/ usr / bin / python的shell脚本时有效。
Typing which python
will tell you what interpreter is being found on your path.
键入哪个python将告诉您路径中找到的解释器。
#2
2
In addition, this can also be resolved by adding your home directory to your Python path. A quick and unobtrusive way to accomplish that is to add it to the PYTHONPATH environment variable (which is generally /app on the Heroku Cedar stack).
此外,还可以通过将主目录添加到Python路径来解决此问题。快速且不显眼的方法是将其添加到PYTHONPATH环境变量(通常是Heroku Cedar堆栈上的/ app)。
Add it via the heroku config command:
通过heroku config命令添加它:
$ heroku config:add PYTHONPATH=/app
That should do it! For more details: http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/
应该这样做!有关详细信息,请访问:http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/
#1
3
You are probably using a different interpreter.
您可能正在使用不同的解释器。
Check to make sure shell python is the same as the one you reference in your script /usr/bin/python . It could be that there is a different one in your path, which would explain why it works when you run python manage.py
but not your shell scrip which you explicitly reference /usr/bin/python
.
检查以确保shell python与脚本/ usr / bin / python中引用的shell相同。可能是你的路径中有一个不同的,这可以解释为什么它在你运行python manage.py而不是你明确引用/ usr / bin / python的shell脚本时有效。
Typing which python
will tell you what interpreter is being found on your path.
键入哪个python将告诉您路径中找到的解释器。
#2
2
In addition, this can also be resolved by adding your home directory to your Python path. A quick and unobtrusive way to accomplish that is to add it to the PYTHONPATH environment variable (which is generally /app on the Heroku Cedar stack).
此外,还可以通过将主目录添加到Python路径来解决此问题。快速且不显眼的方法是将其添加到PYTHONPATH环境变量(通常是Heroku Cedar堆栈上的/ app)。
Add it via the heroku config command:
通过heroku config命令添加它:
$ heroku config:add PYTHONPATH=/app
That should do it! For more details: http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/
应该这样做!有关详细信息,请访问:http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/