本地运行Python程序遇django.core.exceptions.ImproperlyConfigured错误求助
Let’s walk through the likely issues and fixes for your error—this is a common snag when configuring Django for both local development and Heroku deployment.
Possible Solutions
1. Fix ALLOWED_HOSTS Format
Your current ALLOWED_HOSTS includes a full URL (https://stormy-stream-43261.herokuapp.com/) which is invalid. Django expects just the hostname without the protocol or trailing slash:
ALLOWED_HOSTS = ["localhost", "stormy-stream-43261.herokuapp.com"]
When DEBUG=False, Django enforces strict host validation, so this misconfiguration can trigger initialization failures.
2. Correct Your DATABASES Structure
Looking at your settings, your SQLite config has a formatting error. The ENGINE and NAME need to be nested under the default key, and using BASE_DIR ensures the database file path is correct for your project:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'mydatabase.sqlite3'), # Uses project root for the DB file } }
Your current setup places these keys directly in the DATABASES dictionary, which breaks Django’s required configuration structure and can lead to load errors.
3. Isolate django_heroku Configuration
The django_heroku.settings(locals()) line overwrites settings for Heroku’s environment, which can conflict with your local setup. Wrap it in a conditional to only run in production:
import os # Only apply Heroku-specific settings in production if os.environ.get('ENV') == 'production': django_heroku.settings(locals())
Or temporarily comment out this line when testing locally to rule out Heroku-related conflicts.
4. Verify Your wsgi.py File
Double-check that mysite/wsgi.py exists and has the standard content:
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings') application = get_wsgi_application()
If this file is missing, corrupted, or in the wrong directory, Django can’t load the WSGI application.
5. Use DEBUG=True for Local Testing
While debugging locally, set DEBUG = True—this disables strict host checks and simplifies static file handling, making it easier to isolate the root issue:
DEBUG = True
You can flip this back to False when deploying to production.
6. Confirm Dependencies Are Installed
Make sure all required packages are installed and compatible with Django 2.2:
pip install django==2.2 django-heroku dj-database-url whitenoise
Missing or incompatible packages often cause import errors when loading the WSGI application.
After applying these changes, run python manage.py runserver again and see if the error clears up.
内容的提问来源于stack exchange,提问作者Thomas Zhen

