Django部署Heroku遇ImportError:找不到photos.apps模块求助
Hey there! Let's work through this error you're facing—since your Django project runs fine locally with both python manage.py runserver and gunicorn mysite.wsgi, the issue is almost certainly related to how Heroku is handling your project structure, dependencies, or Python path. Here are the most common fixes to try:
1. Double-check your project structure and INSTALLED_APPS setup
First, confirm your local project structure matches what's being deployed to Heroku. It should look something like this (assuming mysite is your git repository root):
mysite/ Procfile requirements.txt mysite/ wsgi.py settings.py ... photos/ apps.py models.py ...
- In your
settings.py, verify the entry inINSTALLED_APPSis correct. If you're using the app config class, it should be'photos.apps.PhotosConfig'—and make sure yourphotos/apps.pyhas the correct class definition:from django.apps import AppConfig class PhotosConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'photos' # This must match the app directory name exactly - If you're not using the app config, you can simplify it to just
'photos'inINSTALLED_APPSto rule out any typos in the config path.
2. Ensure all dependencies are in requirements.txt
Heroku installs dependencies strictly from your requirements.txt file. It's easy to forget to add packages you installed locally. Run this command in your project root to update the file:
pip freeze > requirements.txt
Make sure the file includes Django, gunicorn, and any other packages your project uses. Then commit this file to git before pushing to Heroku.
3. Verify your Procfile location and command
Your Procfile must be in the root of your git repository (not inside the mysite subdirectory). The command web: gunicorn mysite.wsgi is correct only if your wsgi.py lives inside a mysite subdirectory (as in the structure above). If your project structure is different (e.g., wsgi.py is directly in the git root), adjust the command to match (like web: gunicorn wsgi:application).
4. Fix the Python path on Heroku
Sometimes Heroku doesn't automatically add your project root to the Python path, which causes module import errors. You can fix this in two ways:
- Set the PYTHONPATH environment variable via Heroku CLI:
(Replaceheroku config:set PYTHONPATH=/app/mysite/app/mysitewith the path to your project root inside Heroku's filesystem—/appis the default app directory.) - Add the path directly in
settings.py:import os import sys # Add the project root to the Python path sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5. Check your .gitignore file
Accidentally ignoring your photos app directory or mysite subdirectory would mean Heroku never receives those files, leading to missing module errors. Open your .gitignore and make sure there's no line like photos/ or mysite/ (unless those are intentional, which they shouldn't be here).
Start with checking if all your project files are committed to git (run git status to verify)—missing files are the most common culprit here. Then work through the other steps one by one, and you should get past this error!
内容的提问来源于stack exchange,提问作者xiaole kong

