10

I'm trying to run a simple hello world python program on my heroku server. I'm new to heroku.I was able to successfully deploy my script to heroku. My python script and procfile are given below,

hi.py

print("hello world")

Procfile

web: python hi.py

I got "Hello world" as output when i ran heroku run web on my terminal.But when i try to run the app using heroku web url it shows the following error.

Application Error An error occurred in the application and your page could not be served. Please try again in a few moments.

What did i do wrong here? I'm newbie to heroku & its concepts, please do bare.

7
  • 1
    look at your logfiles to see what the error is ... but basically thats not how it works ... the web interface calls a method and expects an httpresponse object .. why not just use flask? Commented Feb 16, 2016 at 17:29
  • Error log shows following , Starting process with command python hi.py app[web.1]: hello heroku[web.1]: State changed from starting to crashed heroku[web.1]: Process exited with status 0 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=jar1.herokuapp.com request_id=f86bf99b-862c-428a-a123-dcfdc1cb29dd fwd="117.245.47.245" dyno= connect= service= status=503 bytes= 2016-02-16T17:37:20.610858+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" Commented Feb 16, 2016 at 17:39
  • I think that means that stdout was printed to and the application exited without returning ... and so it "crashed" with exit code 0 Commented Feb 16, 2016 at 17:42
  • I dont know flask or Django. I just want to sequentially execute a simple python script when i run the app using web URL. is it possible ? Commented Feb 16, 2016 at 17:43
  • 1
    wrong ... since it clearly does not ... (even though you can see it in your logs that it "printed") Commented Feb 16, 2016 at 17:47

2 Answers 2

18

There are three types of dyno configurations available on Heroku:

  • Web -- receives web traffic.
  • Worker -- keeps processing tasks/queues in the background.
  • One-off -- executed once. e.g.: backup.

If you're interested in running a script, do not care about receiving web traffic on it, and don't have a queue to process, then One-off dynos are likely what you'll want to use. This would be useful for database migrations or backups and whatnot.

Minimal example below.


Sample one-off dyno with Heroku and python AKA “hello world”

This assumes you have already created your app on Heroku and are able to use Herolu CLI from the command-line.

A minimal “hello world” Python script would then look like this. Only 2 files required:

  • requirements.txt Required, but can be left empty.
  • task.py with content print("hello world")

Then deploy to Heroku, e.g.:

git add .;
git commit -m "My first commit";
git push heroku master

After that, you'll be able to run your script with heroku run python task.py (and should see the long-awaited hello world in the output.)

If you want to run your program at specific times, use the free Heroku Scheduler add-on.

FYI, Procfile is optional. If you set it to hello: python task.py then you'll be able to run your program with just heroku run hello.

(Note that leaving requirements.txt empty will trigger You must give at least one requirement to install (see "pip help install") warnings on deploy. It's just a warning though and doesn't prevent proper deployment of the program.)

Sign up to request clarification or add additional context in comments.

Comments

8

I disagree and state you want flask

main_app.py

import flask
app = flask.Flask(__name__)

@app.route("/")
def index():
    #do whatevr here...
    return "Hello Heruko"

then change your procfile to web: gunicorn main_app:app --log-file -

1 Comment

Hello @JoranBeasley, I use Google Apps Script to do some tasks and I would like to call a Python code that I deployed to Heroku from GAS. The way you indicated I managed to do this, the problem is that the web is active all the time and consumes all my account dyno hors and I only call the code three times a day. Is there any way to do this without using web leaving the code disabled until I actually call it? I created a question with this question: stackoverflow.com/questions/73172151/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.