TypeError:需要一个整数(得到类型str)

时间:2015-07-30 11:11:29

标签: python flask python-3.4 flask-wtforms babel

我是Python和Flask的新手。我正在学习本教程:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms 现在我收到一个错误,我找不到修复。我重新安装了Python 3.4.3并重新安装了虚拟环境,我已经直接从教程中复制了代码,以确保我在输入时没有出错,但仍然无效。

初始化的.py

from flask import Flask

app = Flask(__name__)
app.config.from_object('config')

from app import views

views.py

from flask import render_template, flash, redirect
from app import app
from .forms import LoginForm

@app.route('/')
@app.route('/index')
def index():
    user = {'nickname': 'Miguel'}
    posts = [
        {
            'author': {'nickname': 'John'},
            'body': 'Beautiful day in Portland!'
        },
        {
            'author': {'nickname': 'Susan'},
            'body': 'The Avengers movie was so cool!'
        }
    ]
    return render_template("index.html",
                           title='Home',
                           user=user,
                           posts=posts)

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    return render_template('login.html',
                           title='Sign In',
                           form=form)

forms.py

from flask.ext.wtf import Form
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired

class LoginForm(Form):
    openid = StringField('openid', validators=[DataRequired()])
    remember_me = BooleanField('remember_me', default=False)

run.py

from app import app
app.run(debug=True)

错误:

(flask) G:\microblog>python run.py
Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from app import app
  File "G:\microblog\app\__init__.py", line 6, in <module>
    from app import views
  File "G:\microblog\app\views.py", line 3, in <module>
    from .forms import LoginForm
  File "G:\microblog\app\forms.py", line 1, in <module>
    from flask.ext.wtf import Form
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
  File "G:\microblog\flask\lib\site-packages\flask\exthook.py", line 62, in load_module
    __import__(realname)
  File "G:\microblog\flask\lib\site-packages\flask_wtf\__init__.py", line 15, in <module>
    from .form import Form
  File "G:\microblog\flask\lib\site-packages\flask_wtf\form.py", line 15, in <module>
    from .i18n import translations
  File "G:\microblog\flask\lib\site-packages\flask_wtf\i18n.py", line 12, in <module>
    from flask_babel import get_locale
  File "G:\microblog\flask\lib\site-packages\flask_babel\__init__.py", line 21, in <module>
    from babel import dates, numbers, support, Locale
  File "G:\microblog\flask\lib\site-packages\babel\dates.py", line 28, in <module>
    from babel.util import UTC, LOCALTZ
  File "G:\microblog\flask\lib\site-packages\babel\util.py", line 278, in <module>
    from babel import localtime
  File "G:\microblog\flask\lib\site-packages\babel\localtime\__init__.py", line 21, in <module>
    from babel.localtime._win32 import _get_localzone
  File "G:\microblog\flask\lib\site-packages\babel\localtime\_win32.py", line 18, in <module>
    tz_names = get_global('windows_zone_mapping')
  File "G:\microblog\flask\lib\site-packages\babel\core.py", line 58, in get_global
    _global_data = pickle.load(fileobj)
TypeError: an integer is required (got type str)

我很沮丧,我无法继续教程,所以欢迎任何帮助。

2 个答案:

答案 0 :(得分:1)

旧版本的babel与3.4不兼容。请改为安装此更新:

pip3.4.exe安装git + https://github.com/mitsuhiko/babel.git@2.0

它适用于Python3.4,因此您不再需要降级Python。

答案 1 :(得分:0)

Python 3.4不支持Babel,而是安装Python 3.3或2.7.10,它会起作用。

- 来自Burkan Khalid的评论