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

Django部署Heroku遇ImportError:找不到photos.apps模块求助

Troubleshooting "ImportError: No module named photos.apps" on Heroku

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 in INSTALLED_APPS is correct. If you're using the app config class, it should be 'photos.apps.PhotosConfig'—and make sure your photos/apps.py has 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' in INSTALLED_APPS to 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:
    heroku config:set PYTHONPATH=/app/mysite
    
    (Replace /app/mysite with the path to your project root inside Heroku's filesystem—/app is 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:33:41