Invalid syntax (SyntaxError) in except handler when using comma
Question:
I have this code:
@app.route('/login/', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
session['username'] = request.form['username']
session['password'] = request.form['password']
try:
# use reddit_api's login
r.login(user=session['username'], password=session['password'])
except InvalidUserPass, e:
error = 'Incorrect username or password. '
if not error:
subreddits = r.user.get_my_reddits(limit=25)
my_reddits = []
for i in range(25):
my_reddits.append(subreddits.next().display_name)
session['my_reddits'] = my_reddits
return redirect(url_for('index'))
return render_template('login.html', error=error)
In 2.x, it worked fine, but in 3.x I get an error message like:
File "app.py", line 101
except InvalidUserPass, e:
^
SyntaxError: invalid syntax
Why does this occur, and how can I fix it?
Answers:
Simply except InvalidUserPass as e:
. And for heaven’s sake, let’s get rid of the ugly error
thing:
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
session['password'] = request.form['password']
try:
# use reddit_api's login
r.login(user=session['username'], password=session['password'])
except InvalidUserPass as e:
return render_template('login.html',
error='Incorrect username or password.')
subreddits = r.user.get_my_reddits(limit=25)
my_reddits = []
for i in range(25):
my_reddits.append(subreddits.next().display_name)
session['my_reddits'] = my_reddits
return redirect(url_for('index'))
return render_template('login.html')
In python3 it’s:
except InvalidUserPass as e:
When getting the error
file /usr/libexec/urlgrabber-ext-down line 28
except oserror, e:
invalid syntax
modify /usr/bin/yum
and /usr/libexec/urlgrabber-ext-dow
files by changing #!/usr/bin/python
to #!/usr/bin/python2
.
issue would be resolved.
In Python 2.x, the syntax except ExampleError, e:
means that exceptions of the type ExampleError
will be caught, and the name e
will be used for that exception inside the except
block.
In 3.x, the closest equivalent syntax is except ExampleError as e:
. (This will also explicitly delete the name e
after the except block has ended, unlike in 2.x where it will remain defined.)
If this error occurs in your own code, simply fix it accordingly.
If this error occurs in library code (example, example), this indicates that either the library does not support modern versions of Python, or else the installation is out of date and upgrading to a newer library version is necessary. Please read the documentation for the library in order to check version compatibility, and do not try to fix it yourself (unless you intend to take over the entire project.)
I have this code:
@app.route('/login/', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
session['username'] = request.form['username']
session['password'] = request.form['password']
try:
# use reddit_api's login
r.login(user=session['username'], password=session['password'])
except InvalidUserPass, e:
error = 'Incorrect username or password. '
if not error:
subreddits = r.user.get_my_reddits(limit=25)
my_reddits = []
for i in range(25):
my_reddits.append(subreddits.next().display_name)
session['my_reddits'] = my_reddits
return redirect(url_for('index'))
return render_template('login.html', error=error)
In 2.x, it worked fine, but in 3.x I get an error message like:
File "app.py", line 101
except InvalidUserPass, e:
^
SyntaxError: invalid syntax
Why does this occur, and how can I fix it?
Simply except InvalidUserPass as e:
. And for heaven’s sake, let’s get rid of the ugly error
thing:
@app.route('/login/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['username'] = request.form['username']
session['password'] = request.form['password']
try:
# use reddit_api's login
r.login(user=session['username'], password=session['password'])
except InvalidUserPass as e:
return render_template('login.html',
error='Incorrect username or password.')
subreddits = r.user.get_my_reddits(limit=25)
my_reddits = []
for i in range(25):
my_reddits.append(subreddits.next().display_name)
session['my_reddits'] = my_reddits
return redirect(url_for('index'))
return render_template('login.html')
In python3 it’s:
except InvalidUserPass as e:
When getting the error
file /usr/libexec/urlgrabber-ext-down line 28
except oserror, e:
invalid syntax
modify /usr/bin/yum
and /usr/libexec/urlgrabber-ext-dow
files by changing #!/usr/bin/python
to #!/usr/bin/python2
.
issue would be resolved.
In Python 2.x, the syntax except ExampleError, e:
means that exceptions of the type ExampleError
will be caught, and the name e
will be used for that exception inside the except
block.
In 3.x, the closest equivalent syntax is except ExampleError as e:
. (This will also explicitly delete the name e
after the except block has ended, unlike in 2.x where it will remain defined.)
If this error occurs in your own code, simply fix it accordingly.
If this error occurs in library code (example, example), this indicates that either the library does not support modern versions of Python, or else the installation is out of date and upgrading to a newer library version is necessary. Please read the documentation for the library in order to check version compatibility, and do not try to fix it yourself (unless you intend to take over the entire project.)