heroku connect python code example

Example 1: run python script on heroku

Sure, you need to do a few things:

Define a requirements.txt file in the root of your project that lists your dependencies. This is what Heroku will use to 'detect' you're using a Python app.

In the Heroku scheduler addon, just define the command you need to run to launch your python script. It will likely be something like python myscript.py.

Finally, you need to have some sort of web server that will listen on the proper Heroku PORT -- otherwise, Heroku will think your app isn't working and it will be in the 'crashed' state -- which isn't what you want. To satisfy this Heroku requirement, you can run a really simple Flask web server like this...

Code (server.py):

from os import environ
from flask import Flask

app = Flask(__name__)
app.run(environ.get('PORT'))
Then, in your Procfile, just say: web: python server.py.

And that should just about do it =)

Example 2: heroku python heroku port issue

import os 
port = int(os.environ.get('PORT', 33507))  # add these lines in code
app.run(host=args.host, port=port, debug=True)  

heroku config:add PORT=33507   # run this command once you upadte the code in terminal