19

我是 Python 的初学者,一直在测试不同类型的示例代码。当我开始使用 Python3 而不是 2.7 时;我遇到了语法错误,但我不明白如何修复该错误。

  File "app.py", line 101
    except InvalidUserPass, e:
                          ^
SyntaxError: invalid syntax

这里有更多的代码来提供一些上下文:

@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)
4

4 回答 4

31

改变

except InvalidUserPass, e:

except InvalidUserPass as e:

有关更多信息,请参阅内容。

于 2012-09-20T19:28:50.240 回答
8

简直了except InvalidUserPass as e:。看在上帝的份上,让我们摆脱丑陋error的东西:

@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')
于 2012-09-20T19:27:34.743 回答
2

在python3中它是:

except InvalidUserPass as e:
于 2012-09-20T19:28:54.100 回答
0
file /usr/libexec/urlgrabber-ext-down line 28 except oserror e invalid syntax

modify /usr/bin/yum and /usr/libexec/urlgrabber-ext-dow files

#!/usr/bin/python to #!/usr/bin/python2

问题将得到解决。

于 2018-08-21T07:58:12.567 回答