无法在 Heroku 上导入芹菜

Posted

技术标签:

【中文标题】无法在 Heroku 上导入芹菜【英文标题】:Cannot import celery on Heroku 【发布时间】:2018-12-10 22:58:02 【问题描述】:

当我运行“heroku local”时,我可以看到 celery 任务正在窗口中运行。但是,当我尝试将代码部署到 Heroku 时,我不断收到无法找到模块“celery”的错误消息。此外,我已以应避免命名冲突的方式命名我的文件。

项目结构

invmvp
    --__init.py__
    --celeryapp.py
    --settings.py
    --urls.py
    --wsgi.py

过程文件

worker: celery worker -A invmvp --app=invmvp.celeryapp:app

Pipfile

[[source]]

url = "https://pypi.python.org/simple"
verify_ssl = true


[packages]

celery = "*"
django = "*"
gunicorn = "*"
django-heroku = "*"
requests = "*"
yahoo-finance = "*"
whitenoise = "*"
npm = "*"


[requires]

python_version = "3.6"

__init__.py

from __future__ import absolute_import, unicode_literals

__all__ = ('celery_app')

celeryapp.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery    

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'invmvp.settings')

app = Celery('invmvp')


app.conf.update(BROKER_URL=os.environ["REDIS_URL"],
                CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])

app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: 0!r'.format(self.request))

我尝试推送到 heroku 时的输出

Counting objects: 88, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (83/83), done.
Writing objects: 100% (88/88), 14.26 KiB | 3.56 MiB/s, done.
Total 88 (delta 53), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote:        Skipping installation, as Pipfile.lock hasn't changed since last deploy.
remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 15, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
remote:            settings.INSTALLED_APPS
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
remote:            self._setup(name)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
remote:            self._wrapped = Settings(settings_module)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
remote:            mod = importlib.import_module(self.SETTINGS_MODULE)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "<frozen importlib._bootstrap>", line 994, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 971, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 665, in _load_unlocked

remote:          File "<frozen importlib._bootstrap_external>", line 678, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "/tmp/build_9ccdb12ad149602e24a09bf3bf297f98/invmvp/__init__.py", line 10, in <module>
remote:            import celery
remote:        ModuleNotFoundError: No module named 'celery'
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to staging-invmvp.
remote: 
To https://git.heroku.com/staging-invmvp.git
 ! [remote rejected] celery -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/staging-invmvp.git'

【问题讨论】:

你安装了 Celery 吗?显示您的 requirements.txt 或 Pipfile。 我尝试重新安装并解决了问题。感谢您的帮助! 【参考方案1】:

我尝试重新安装 celery 并解决了问题。

pipenv install celery

【讨论】:

以上是关于无法在 Heroku 上导入芹菜的主要内容,如果未能解决你的问题,请参考以下文章

芹菜中的导入错误

导入 matplotlib 失败,在 heroku 上没有名为 _tkinter 的模块

Heroku 应用程序因 eslint 错误导入而崩溃

如何在不杀死未完成的芹菜任务的情况下重新启动heroku应用程序

无法将本地 Postgres 数据库推送到 Heroku

Axios GET请求在我的本地版本上运行,但在Heroku上失败-未捕获(承诺)错误:请求失败,状态码为500