You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Heroku部署Django项目遇ModuleNotFoundError: No module named 'austbooks'求助

Django项目部署Heroku失败问题

问题概述

本地Windows10环境下运行python manage.py runserver一切正常,但部署到Heroku时触发ModuleNotFoundError: No module named 'austbooks'错误。尝试执行heroku config:set DISABLE_COLLECTSTATIC=1后部署成功,但应用访问仍报错,日志显示相同的模块找不到问题,已重置该配置。

项目结构

austbooks
├── accounting
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   │   └── accounting
│   │       ├── css
│   │       └── js
│   ├── templates
│   │   ├── account
│   │   └── accounting
│   ├── tests.py
│   ├── views.py
│   ├── __init__.py
│   └── __pycache__
├── austbooks
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
│   ├── __init__.py
│   └── __pycache__
├── manage.py
├── Procfile
├── requirements.txt
├── runtime.txt
└── staticfiles
    ├── Accounting
    │   ├── css
    │   └── js
    ├── admin
    │   ├── css
    │   ├── img
    │   └── js
    ├── debug_toolbar
    │   ├── css
    │   └── js
    └── staticfiles.json

部署错误日志

-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
         File "/app/.heroku/python/lib/python3.12/site-packages/django/core/management/__init__.py", line 255, in fetch_command
           app_name = commands[subcommand]
                      ~~~~~~~~^^^^^^^^^^^^
       KeyError: 'collectstatic'
       During handling of the above exception, another exception occurred:
       Traceback (most recent call last):
         File "/tmp/build_20b80918/manage.py", line 22, in <module>
           main()
         File "/tmp/build_20b80918/manage.py", line 18, in main
           execute_from_command_line(sys.argv)
         File "/app/.heroku/python/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
           utility.execute()
         File "/app/.heroku/python/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
           self.fetch_command(subcommand).run_from_argv(self.argv)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         File "/app/.heroku/python/lib/python3.12/site-packages/django/core/management/__init__.py", line 262, in fetch_command
           settings.INSTALLED_APPS
         File "/app/.heroku/python/lib/python3.12/site-packages/django/conf/__init__.py", line 89, in __getattr__
           self._setup(name)
         File "/app/.heroku/python/lib/python3.12/site-packages/django/conf/__init__.py", line 76, in _setup
           self._wrapped = Settings(settings_module)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^
         File "/app/.heroku/python/lib/python3.12/site-packages/django/conf/__init__.py", line 190, in __init__
           mod = importlib.import_module(self.SETTINGS_MODULE)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         File "/app/.heroku/python/lib/python3.12/importlib/__init__.py", line 90, in import_module
           return _bootstrap._gcd_import(name[level:], package, level)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
         File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
         File "<frozen importlib._bootstrap>", line 1304, in _find_and_load_unlocked
         File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
         File "<frozen importlib._bootstrap>", line 1381, in _gcd_import
         File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
         File "<frozen importlib._bootstrap>", line 1318, in _find_and_load_unlocked
       ModuleNotFoundError: No module named 'austbooks'
 !     Error while running '$ python manage.py collectstatic --noinput'.
       See traceback above for details.
       You may need to update application code to resolve this error.
       Or, you can disable collectstatic for this application:
          $ heroku config:set DISABLE_COLLECTSTATIC=1
 !     Push rejected, failed to compile Python app.
 !     Push failed

已尝试操作

  • 执行heroku config:set DISABLE_COLLECTSTATIC=1后部署成功,但应用访问报错,日志仍显示模块找不到错误,已重置该配置。
  • 修改Procfile为web: gunicorn austbooks.wsgi --log-file -以及web: gunicorn --pythonpath GroomingService GroomingService.wsgi(注:GroomingService为笔误,实际应为austbooks)。
  • 修改wsgi.py中的os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'austbooks.austbooks.settings')。
  • 检查所有文件名和配置项的拼写及大小写,未发现问题。

关键配置文件内容

Procfile

web: gunicorn austbooks.wsgi

requirements.txt

asgiref==3.7.2
beautifulsoup4==4.12.2
certifi==2023.7.22
cffi==1.16.0
charset-normalizer==3.3.2
cryptography==41.0.5
defusedxml==0.7.1
dj-database-url==2.1.0
Django==5.0
django-allauth==0.58.2
django-debug-toolbar==4.2.0
django-heroku==0.3.1
gunicorn==21.2.0
idna==3.4
oauthlib==3.2.2
packaging==23.2
psycopg2==2.9.9
pycparser==2.21
PyJWT==2.8.0
python3-openid==3.2.0
requests==2.31.0
requests-oauthlib==1.3.1
soupsieve==2.5
sqlparse==0.4.4
typing_extensions==4.8.0
tzdata==2023.3
urllib3==2.1.0
whitenoise==6.6.0

runtime.txt

python-3.12.0

settings.py(关键部分)

from pathlib import Path
import os
import django_heroku
import dj_database_url

BASE_DIR = Path(__file__).resolve().parent.parent

IS_HEROKU_APP = "DYNO" in os.environ and not "CI" in os.environ

INSTALLED_APPS = [
    # Use WhiteNoise's runserver implementation instead of the Django default, for dev-prod parity.
    "whitenoise.runserver_nostatic",
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'accounting',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'debug_toolbar',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'allauth.account.middleware.AccountMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]

ROOT_URLCONF = 'austbooks.urls'

WSGI_APPLICATION = 'austbooks.wsgi.application'

AUTHENTICATION_BACKENDS = [
    'allauth.account.auth_backends.AuthenticationBackend',
    'allauth.account.auth_backends.ModelBackend',
]


STATIC_URL = 'static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'accounting', 'static'),
)
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

# Static root for Heroku
STATIC_ROOT = BASE_DIR / 'staticfiles'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


# Heroku: Update database configuration from $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Activate Django-Heroku.
django_heroku.settings(locals())

wsgi.py

import os
from django.core.wsgi import get_wsgi_application
from whitenoise import WhiteNoise

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

application = get_wsgi_application()
application = WhiteNoise(application)

解决方案

问题核心在于wsgi.py中的设置模块路径错误,以及项目结构识别问题:

  1. 修正wsgi.py中的设置模块路径
    当前wsgi.py中设置的austbooks.austbooks.settings是错误的,根据项目结构,settings.py位于根目录下的austbooks子文件夹中,正确路径应为austbooks.settings。修改后代码:

    import os
    from django.core.wsgi import get_wsgi_application
    from whitenoise import WhiteNoise
    
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'austbooks.settings')
    
    application = get_wsgi_application()
    application = WhiteNoise(application)
    
  2. 确认Git仓库结构
    确保推送的代码根目录是包含manage.py的文件夹,避免嵌套的austbooks目录导致Heroku无法识别项目模块。

  3. 重新部署
    提交修改后的代码并推送到Heroku:

    git add . && git commit -m "Fix wsgi settings path"
    git push heroku main
    

内容的提问来源于stack exchange,提问作者Abdul126

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 14:02:31