Django在Heroku上:ModuleNotFoundError:没有名为“ app_name”的模块

时间:2019-07-13 08:01:45

标签: django python-3.x heroku django-rest-framework

在将带有django rest框架的Django应用程序部署到heroku时遇到了一些麻烦。奇怪的是,推送和部署到Heroku不会有问题,但是部署后会崩溃。这是我的文件结构:

Include/
man/
Procfile/
requirements.txt
runtime.txt
Scripts/
tcl/
webadvisorapi
    │   manage.py
    ├───src
    │   │   admin.py
    │   │   apps.py
    │   │   models.py
    │   │   serializers.py
    │   │   tests.py
    │   │   urls.py
    │   │   views.py
    │   │   __init__.py
    │   │
    │   ├───management
    │   │   │   __init__.py
    │   │   │
    │   │   └───commands
    │   │           runrequest.py
    │   │           __init__.py
    │   │
    │   ├───scripts
    │           request.py
    │           __init__.py
    │
    ├───static
    │       .keep
    │
    ├───staticfiles
    └───webadvisorapi
        │   settings.py
        │   urls.py
        │   wsgi.py
        │   __init__.py
        │
        ├───static
        │       .keep

我已经在自己的机器上运行了该程序,并且运行良好。有趣的是,当我使用src.apps.SrcConfig中除INSTALLED_APPS以外的其他路径时,无法将我的应用程序推送并部署到Heroku,因为它为我提供了相同的ModuleNotFoundError。除此之外,我确实获得了成功的部署,但是该应用程序崩溃了。我什至从heroku克隆了我的项目,然后在本地运行我的项目,没有问题。这是来自heroku日志--tail的日志:

2019-07-13T06:43:31.172615+00:00 heroku[web.1]: Starting process with command `gunicorn webadvisorapi.webadvisorapi.wsgi --log-file -`
2019-07-13T06:43:33.243340+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [4] [INFO] Starting gunicorn 19.9.0
2019-07-13T06:43:33.243849+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [4] [INFO] Listening at: http://0.0.0.0:34512 (4)
2019-07-13T06:43:33.243940+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [4] [INFO] Using worker: sync
2019-07-13T06:43:33.249546+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [10] [INFO] Booting worker with pid: 10
2019-07-13T06:43:33.350998+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [11] [INFO] Booting worker with pid: 11
2019-07-13T06:43:33.524337+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [10] [ERROR] Exception in worker
process
2019-07-13T06:43:33.524355+00:00 app[web.1]: Traceback (most recent call last):
2019-07-13T06:43:33.524394+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-07-13T06:43:33.524397+00:00 app[web.1]: worker.init_process()
2019-07-13T06:43:33.524399+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-07-13T06:43:33.524401+00:00 app[web.1]: self.load_wsgi()
2019-07-13T06:43:33.524403+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-07-13T06:43:33.524405+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2019-07-13T06:43:33.524413+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-07-13T06:43:33.524415+00:00 app[web.1]: self.callable = self.load()
2019-07-13T06:43:33.524417+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-07-13T06:43:33.524419+00:00 app[web.1]: return self.load_wsgiapp()
2019-07-13T06:43:33.524421+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-07-13T06:43:33.524423+00:00 app[web.1]: return util.import_app(self.app_uri)
2019-07-13T06:43:33.524425+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
2019-07-13T06:43:33.524426+00:00 app[web.1]: __import__(module)
2019-07-13T06:43:33.524428+00:00 app[web.1]: File "/app/webadvisorapi/webadvisorapi/wsgi.py", line 17, in
<module>
2019-07-13T06:43:33.524430+00:00 app[web.1]: application = get_wsgi_application()
2019-07-13T06:43:33.524432+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2019-07-13T06:43:33.524434+00:00 app[web.1]: django.setup(set_prefix=False)
2019-07-13T06:43:33.524436+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
2019-07-13T06:43:33.524438+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)
2019-07-13T06:43:33.524440+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/registry.py", line 89, in populate
2019-07-13T06:43:33.524442+00:00 app[web.1]: app_config = AppConfig.create(entry)
2019-07-13T06:43:33.524444+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/config.py", line 116, in create
2019-07-13T06:43:33.524445+00:00 app[web.1]: mod = import_module(mod_path)
2019-07-13T06:43:33.524447+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
2019-07-13T06:43:33.524454+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2019-07-13T06:43:33.524457+00:00 app[web.1]: ModuleNotFoundError: No module named 'src'
2019-07-13T06:43:33.524572+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [10] [INFO] Worker exiting (pid:
10)
2019-07-13T06:43:33.676529+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [11] [ERROR] Exception in worker
process
2019-07-13T06:43:33.676532+00:00 app[web.1]: Traceback (most recent call last):
2019-07-13T06:43:33.676535+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-07-13T06:43:33.676537+00:00 app[web.1]: worker.init_process()
2019-07-13T06:43:33.676538+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-07-13T06:43:33.676540+00:00 app[web.1]: self.load_wsgi()
2019-07-13T06:43:33.676541+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-07-13T06:43:33.676543+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2019-07-13T06:43:33.676545+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-07-13T06:43:33.676547+00:00 app[web.1]: self.callable = self.load()
2019-07-13T06:43:33.676549+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-07-13T06:43:33.676550+00:00 app[web.1]: return self.load_wsgiapp()
2019-07-13T06:43:33.676552+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-07-13T06:43:33.676553+00:00 app[web.1]: return util.import_app(self.app_uri)
2019-07-13T06:43:33.676555+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py", line 350, in import_app
2019-07-13T06:43:33.676556+00:00 app[web.1]: __import__(module)
2019-07-13T06:43:33.676558+00:00 app[web.1]: File "/app/webadvisorapi/webadvisorapi/wsgi.py", line 17, in
<module>
2019-07-13T06:43:33.676559+00:00 app[web.1]: application = get_wsgi_application()
2019-07-13T06:43:33.676561+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2019-07-13T06:43:33.676562+00:00 app[web.1]: django.setup(set_prefix=False)
2019-07-13T06:43:33.676564+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
2019-07-13T06:43:33.676565+00:00 app[web.1]: apps.populate(settings.INSTALLED_APPS)
2019-07-13T06:43:33.676567+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/registry.py", line 89, in populate
2019-07-13T06:43:33.676568+00:00 app[web.1]: app_config = AppConfig.create(entry)
2019-07-13T06:43:33.676569+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/apps/config.py", line 116, in create
2019-07-13T06:43:33.676571+00:00 app[web.1]: mod = import_module(mod_path)
2019-07-13T06:43:33.676572+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
2019-07-13T06:43:33.676574+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2019-07-13T06:43:33.676576+00:00 app[web.1]: ModuleNotFoundError: No module named 'src'
2019-07-13T06:43:33.676797+00:00 app[web.1]: [2019-07-13 06:43:33 +0000] [11] [INFO] Worker exiting (pid:
11)
2019-07-13T06:43:33.743217+00:00 app[web.1]: Traceback (most recent call last):
2019-07-13T06:43:33.743229+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 210, in run
2019-07-13T06:43:33.743551+00:00 app[web.1]: self.sleep()
2019-07-13T06:43:33.743557+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 360, in sleep
2019-07-13T06:43:33.743786+00:00 app[web.1]: ready = select.select([self.PIPE[0]], [], [], 1.0)
2019-07-13T06:43:33.743792+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 245, in handle_chld
2019-07-13T06:43:33.743969+00:00 app[web.1]: self.reap_workers()
2019-07-13T06:43:33.744000+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2019-07-13T06:43:33.744238+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2019-07-13T06:43:33.744322+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2019-07-13T06:43:33.744327+00:00 app[web.1]:
2019-07-13T06:43:33.744330+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-07-13T06:43:33.744332+00:00 app[web.1]:
2019-07-13T06:43:33.744336+00:00 app[web.1]: Traceback (most recent call last):
2019-07-13T06:43:33.744346+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 11, in <module>
2019-07-13T06:43:33.744482+00:00 app[web.1]: sys.exit(run())
2019-07-13T06:43:33.744486+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 61, in run
2019-07-13T06:43:33.744632+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2019-07-13T06:43:33.744639+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 223, in run
2019-07-13T06:43:33.744843+00:00 app[web.1]: super(Application, self).run()
2019-07-13T06:43:33.744850+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 72, in run
2019-07-13T06:43:33.744985+00:00 app[web.1]: Arbiter(self).run()
2019-07-13T06:43:33.744991+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 232, in run
2019-07-13T06:43:33.745162+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status)
2019-07-13T06:43:33.745168+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 345, in halt
2019-07-13T06:43:33.745377+00:00 app[web.1]: self.stop()
2019-07-13T06:43:33.745411+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 393, in stop
2019-07-13T06:43:33.745611+00:00 app[web.1]: time.sleep(0.1)
2019-07-13T06:43:33.745617+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 245, in handle_chld
2019-07-13T06:43:33.745787+00:00 app[web.1]: self.reap_workers()
2019-07-13T06:43:33.745793+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2019-07-13T06:43:33.746031+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2019-07-13T06:43:33.746074+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

settings.py(我忽略了我认为不相关的内容)

import django_heroku
import dj_database_url
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'src.apps.SrcConfig',
    'corsheaders',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

ROOT_URLCONF = 'webadvisorapi.urls'

WSGI_APPLICATION = 'webadvisorapi.wsgi.application'

STATIC_URL = '/static/'

django_heroku.settings(locals()) 

Procfile

web: gunicorn webadvisorapi.webadvisorapi.wsgi --log-file -

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

apps.py

from django.apps import AppConfig

class SrcConfig(AppConfig):
    name = 'src'

webadvisorapi / urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('src.urls')),
]

鉴于此路径在我的本地主机中运行正常,因此我真的不确定这是什么问题。我将不胜感激。

0 个答案:

没有答案