在 Heroku 上运行 Django 自定义 manage.py 任务 - 导入问题

发布于 2024-12-19 15:35:58 字数 1492 浏览 6 评论 0原文

我正在尝试在 Heroku 上运行自定义 django 命令作为计划任务。我可以通过以下方式在本地执行自定义命令:python manage.py send_daily_email。 (注意:我的自定义管理命令本身没有任何问题)

但是,当尝试通过 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

我在 /bin/ 中放置了一个 python 脚本send_daily_email.py,其内容如下:

#! /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

但是,在 Heroku 中,我能够运行 heroku run bin/python - 启动 python shell - 并成功导入 settings< /代码> 来自django.conf

我很确定它与我的 PYTHON_PATH 或 Django 的 SETTINGS_MODULE 的可见性有关,但我不确定如何解决的问题。有人能指出我正确的方向吗?有没有更简单的方法来完成我在这里想做的事情?

非常感谢您提前提供的提示和建议! Heroku 新手! :)

编辑:

根据 Nix 的评论,我做了一些调整,并且确实发现指定我的确切 python 路径,我确实通过了 Django 设置。

我现在收到:

  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'

虽然,当我运行“heroku run bin/python app/manage.py”时,我可以看到“send_daily_email”。

如果我找到答案,我会及时更新。

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)

However, Heroku is giving me the following exception when trying to "Run" the task through Heroku Scheduler addon:

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:

#! /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

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?

Thank you so much for your tips and advice in advance! New to 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.

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```.

I'll keep an update if I come across the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-12-26 15:35:58

您可能正在使用不同的解释器。

检查以确保 shell python 与您在脚本 /usr/bin/python 中引用的 shell python 相同。您的路径中可能有一个不同的路径,这可以解释为什么它在您运行 python manage.py 时起作用,但在您显式引用 /usr/bin/ 的 shell 脚本中不起作用蟒蛇。


输入 which python 将告诉您在您的路径上找到了哪个解释器。

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.


Typing which python will tell you what interpreter is being found on your path.

鸵鸟症 2024-12-26 15:35:58

另外,也可以通过将你的主目录添加到你的Python路径来解决这个问题。实现此目的的一种快速且不显眼的方法是将其添加到 PYTHONPATH 环境变量(通常是 Heroku Cedar 堆栈上的 /app )。

通过 heroku config 命令添加它:

$ heroku config:add PYTHONPATH=/app

应该可以了!有关更多详细信息: http://tomatohater.com/ 2012/01/17/自定义-django-management-commands-on-heroku/

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).

Add it via the heroku config command:

$ heroku config:add PYTHONPATH=/app

That should do it! For more details: http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文