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

部署Django应用到Heroku时遇ModuleNotFoundError:找不到rest_framework

Fixing ModuleNotFoundError for rest_framework on Heroku Deployment

Hey there, let's get this sorted out! The issue you're hitting is super common when deploying Django apps to Heroku—Heroku relies entirely on your requirements.txt file to install all the Python packages your app needs, and it looks like djangorestframework isn't listed there (or the file wasn't properly committed to git).

Here's exactly what to do to fix it:

  • Step 1: Update your requirements.txt with the missing dependency
    Open your local project terminal and run this command to ensure all your installed packages (including djangorestframework) are added to the file:

    pip freeze > requirements.txt
    

    If you prefer to specify a specific version manually, you can add this line directly to the file:

    djangorestframework==3.14.0  # Replace with the version you're using locally
    
  • Step 2: Commit the updated requirements.txt to git
    Heroku only uses files that are tracked in your git repository, so stage and commit the changes:

    git add requirements.txt
    git commit -m "Add djangorestframework to requirements for Heroku deployment"
    
  • Step 3: Push to Heroku again
    Now re-run your push command, and Heroku should install the missing package during the build process:

    git push heroku master
    

If you still run into issues:

If the error persists even after adding the dependency, it might be due to cached build assets on Heroku. Clear the cache with this command, then push again:

heroku builds:cache:purge

Your original push log for reference:

$ git push heroku master
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 704 bytes | 704.00 KiB/s, done.
Total 8 (delta 6), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing pip
remote: Skipping installation, as Pi...
(Followed by ModuleNotFoundError: No module named 'rest_framework')

内容的提问来源于stack exchange,提问作者C. Joseph

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:34:18