I have a hard time finding documentation for creating Procfiles using flask with gunicorn and Heroku. Somewhere I found that the syntax is:
web: gunicorn my_folder.my_module:app
. But I can't make it work. It only works for me when my python script: hello.py
is in the root folder of the app. When I put it in a subfolder called app and create a Procfile: web: gunicorn app.hello:app
it doesn't work. Only when I use web: gunicorn hello:app
and my python script is in the root folder. Can someone explain me the proper syntax of Procfiles for gunicorn on Heroku, and how to make it work when the python script is in a subfolder?
Asked
Active
Viewed 2.0k times
19

CasperTN
- 441
- 1
- 5
- 14
-
Have you added the `__init__.py` file to the the app folder to make it a Python module? – Jean Jung Aug 09 '16 at 13:08
-
No haven't added that.. – CasperTN Aug 09 '16 at 13:15
-
And it works when you add that file? – Jean Jung Aug 09 '16 at 13:18
-
And is that an empty folder or should it contain some code? – CasperTN Aug 09 '16 at 13:25
-
1Just an empty file is Ok, but I am guessing here, I don't know Heroku nor how Procfile works, I just know that python requires `__init__.py` to see a folder as a Python package. – Jean Jung Aug 09 '16 at 13:29
-
You put `unicorn` in your Procfile instead of `gunicorn`. – dirn Aug 09 '16 at 14:12
-
No that was the bloody autocorrect that did that.. In my post. – CasperTN Aug 09 '16 at 14:24
-
Have a look at [this template](https://github.com/zachwill/flask_heroku). It has a Procfile and is ready to be pushed to Heroku. You can learn from it, or just fork it and base your app on the template. – 0x60 Aug 09 '16 at 14:04
2 Answers
35
Gunicorn takes a flag, --chdir
, that lets you select which directory your Python app lives in. So, if you have a directory structure like:
my-project/
Procfile
my_folder/
my_module.py
and my_module.py
contains:
app = Flask(__name__, ...)
You can put the following in your Procfile
:
web: gunicorn --chdir my_folder my_module:app

kris
- 23,024
- 10
- 70
- 79
-
1your comment save me for many hours of debugging ! kudos can you explained well how my_module:app works? – Espoir Murhabazi Sep 25 '17 at 16:14
-
-
10`web: gunicorn app:app` The first `app` represents the name of the python file that runs your application or the name of the module it is in. The second `app` represents the app name that is named in your .py file. Just wanted to add because it helps clarify the contents of the procfile and it's syntax. E.g. your appname would be my_awesome_app in the following code: `if __name__ == '__main__':` `my_awesome_app.run()` – Gerardsson May 07 '20 at 10:14
-
Do we have a provision to add the port number here as well? Example: `web: gunicorn wsgi:app port:8080` – Biswajit Roy Nov 26 '21 at 03:35
-
This is finally what solved my problem when trying to deploy to Heroku and getting the H10 error. Thank you! – supersaidso Dec 14 '22 at 17:23
0
You may try Hypercorn as it is successor of Gunicorn
Modify your Procfile as follows :
web: hypercorn -b 0.0.0.0:$PORT Path_To_Your_File:app

Nithish Reddy
- 1
- 1