部署Django应用到Heroku时遇ModuleNotFoundError:找不到rest_framework
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.txtwith the missing dependency
Open your local project terminal and run this command to ensure all your installed packages (includingdjangorestframework) are added to the file:pip freeze > requirements.txtIf 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 locallyStep 2: Commit the updated
requirements.txtto 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

