Flask Gunicorn app cannot get __name__ equal to '__main__'

I have this from /home/myname/myapp/app.py :

 from flask import Flask app = Flask(__name__) print __name__ @app.route('/') def index(): return "Hello world!" if __name__ == '__main__': print 'in if' app.run() 

When I run:

 $ gunicorn app:app -b 127.0.0.2:8000 

It says:

 2013-03-01 11:26:56 [21907] [INFO] Starting gunicorn 0.17.2 2013-03-01 11:26:56 [21907] [INFO] Listening at: http://127.0.0.2:8000 (21907) 2013-03-01 11:26:56 [21907] [INFO] Using worker: sync 2013-03-01 11:26:56 [21912] [INFO] Booting worker with pid: 21912 app 

So the __name__ app is app . Not __main__ , as I need to run the if statement.

I tried putting an empty __init__.py into the directory. Here is my nginx sites-enabled default :

 server { #listen 80; ## listen for ipv4; this line is default and implied #listen [::]:80 default_server ipv6only=on; ## listen for ipv6 root /home/myname/myapp; # Make site accessible from http://localhost/ server_name localhost; location / { proxy_pass http://127.0.0.2:8000; } } 

Edit

... While this application is printing 'Hello world' when visiting the site. The thing is, I need __name__ to equal '__main__' . I also just want to know why this is not so and how to make it equal to __main__ .

Edit 2

... I just had the epiphany that I did not need to run app.run() , since this requires Gunicorn. Duh. But I still would like to find out why __name__ not '__main__'
+4
source share
1 answer

Python sets __name__ to "__main__" when the script is the entry point for the Python interpreter. Since Gunicorn imports the script, it works, but the script will not be an entry point and therefore will not be __name__ set to "__main__" .

+6
source

All Articles