Flask应用部署Heroku遇问题:无法兼容Python Buildpack
Here are actionable steps to resolve the "App not compatible with buildpack" error:
1. Verify Procfile Correctness
- Ensure the Procfile is in the root directory of your git repo, has no file extension, and uses the exact name
Procfile(uppercase 'P'). - Check that its content follows the proper format for Flask apps:
Replaceweb: gunicorn <your-flask-module>:<flask-app-instance><your-flask-module>with the name of your main Flask file (e.g.,appif your file isapp.py) and<flask-app-instance>with the name of your Flask object (e.g.,appif you haveapp = Flask(__name__)).
2. Confirm Files Are Tracked in Git
Even if files exist locally, they won’t be sent to Heroku unless committed to git:
- Run
git statusto check ifProcfileandrequirements.txtare listed under "Changes to be committed" or "Untracked files". - If untracked, add them with:
git add Procfile requirements.txt git commit -m "Add Procfile and requirements.txt" - Push again with
git push heroku main.
3. Specify Python Version Explicitly
Heroku may default to an older Python version if not specified. Add a runtime.txt file to your root directory with:
python-3.12.1
Commit this file and push to Heroku. This ensures Heroku uses the same Python version as your local environment.
4. Clean Up requirements.txt
Your requirements.txt includes platform-specific packages (like pywin32, pywinpty) that are incompatible with Heroku’s Linux environment. Remove these entries, then test installing dependencies locally with:
pip install -r requirements.txt
Fix any installation errors before pushing to Heroku.
5. Check Repository Structure
Ensure your git repository root contains all necessary files (Procfile, requirements.txt, main Flask app files). Avoid nesting your app in a subdirectory—Heroku expects core files at the root for proper buildpack detection.
6. Reset Buildpacks
Manually resetting the buildpack can resolve detection glitches:
heroku buildpacks:clear heroku buildpacks:set heroku/python git push heroku main
If the issue persists, run heroku logs --tail to pull full build logs for more detailed error clues.
内容的提问来源于stack exchange,提问作者pyguypy

