Search code examples
pythonpython-2.7flasktraceback

Keep getting traceback call for app.run() in Flask


So I'm working on this project that I'm using Flask for and I'm still new to using Flask. However, whenever I try running the script I'm using for this project I get a traceback call complaining about app.run().

Here's the code from my script:

import os
from flask import Flask, render_template, request, flash, redirect, url_for
from flask.ext.assets import Environment, Bundle
from flask_wtf import Form 
from wtforms import TextField, TextAreaField, SubmitField
from wtforms.validators import InputRequired

CSRF_ENABLED = True
app = Flask(__name__)
app.secret_key = 'development key'
assets = Environment(app)
assets.url = app.static_url_path
scss = Bundle('assets/scss/app.scss', filters='scss', output='css/app.css')
assets.register('app_scss', scss)

@app.route('/')
def server_1():
     return render_template('server_1.html')


if __name__ == '__main__':
      app.run()

Here's the traceback that the script keeps outputting (I spaced out the individual tracebacks to make them easier to read):

$python app.py
 * Running on http://127.0.0.1:5000/
 Traceback (most recent call last):
 File "app.py", line 22, in <module>
   app.run()

 File "/Library/Python/2.7/site-packages/flask/app.py", line 772, in run
    run_simple(host, port, self, **options)

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-  py2.7.egg/werkzeug/serving.py", line 710, in run_simple
 inner()

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/serving.py", line 692, in inner
passthrough_errors, ssl_context).serve_forever()

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/serving.py", line 486, in make_server
passthrough_errors, ssl_context)

 File "/Library/Python/2.7/site-packages/Werkzeug-0.9.6-py2.7.egg/werkzeug/serving.py", line 410, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 419, in __init__
self.server_bind()

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)

socket.error: [Errno 48] Address already in use

I'm not too sure what I did wrong so any help that can be given is greatly appreciated.


Solution

  • The final error:

    socket.error: [Errno 48] Address already in use
    

    Indicates that you already have something else running on 127.0.0.1 port 5000.

    To find out what process is using this address, you can run the following command (on OS X)

    lsof -i tcp:5000
    

    Alternatively, if you wish to use a different port, say 5001, adjust your code as such:

    if __name__ == '__main__':
          app.run(port=5001)