Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python/flask: TypeError: an integer is required (got type str)

Tags:

python

flask

I am brand new to python and flask. I am trying to work my way through the flask tutorial at http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world. I have run into an error that I cannot figure out. I have exhaustively (I think) searched for a solution, but I can't find one that fits my problem. I have uninstalled and reinstalled flask. I even created a new venv and started all over again, but nothing has worked.

I'm trying to build a simple web form app. I created a form like:

from flask_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)

When I import LoginForm, I get the error

TypeError: an integer is required (got type str)

The full stacktrace is here:

C:\microblog\flask\Scripts\python.exe C:/microblog/run.py
Traceback (most recent call last):
  File "C:/microblog/run.py", line 2, in <module>
    from app import app
  File "C:\microblog\app\__init__.py", line 6, in <module>
    from app import views
  File "C:\microblog\app\views.py", line 5, in <module>
    from .forms import LoginForm
  File "C:\microblog\app\forms.py", line 3, in <module>
    from flask_wtf import Form
  File "C:\microblog\flask\lib\site-packages\flask_wtf\__init__.py", line 15, in <module>
    from .form import Form
  File "C:\microblog\flask\lib\site-packages\flask_wtf\form.py", line 15, in <module>
    from .i18n import translations
  File "C:\microblog\flask\lib\site-packages\flask_wtf\i18n.py", line 12, in <module>
    from flask_babel import get_locale
  File "C:\microblog\flask\lib\site-packages\flask_babel\__init__.py", line 21, in <module>
    from babel import dates, numbers, support, Locale
  File "C:\microblog\flask\lib\site-packages\babel\dates.py", line 28, in <module>
    from babel.util import UTC, LOCALTZ
  File "C:\microblog\flask\lib\site-packages\babel\util.py", line 278, in <module>
    from babel import localtime
  File "C:\microblog\flask\lib\site-packages\babel\localtime\__init__.py", line 21, in <module>
from babel.localtime._win32 import _get_localzone
  File "C:\microblog\flask\lib\site-packages\babel\localtime\_win32.py", line 18, in <module>
    tz_names = get_global('windows_zone_mapping')
  File "C:\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)

Any help is appreciated - this is driving me nuts!

like image 792
dstaraster Avatar asked Jul 29 '15 18:07

dstaraster


2 Answers

This is build error in the babel 2.0 package on the python package index

https://github.com/mitsuhiko/babel/issues/174

To summarize, a pickled file babel/global.dat is included in a the package and this can't be read by python 3 because it was created by script running under python 2.

I resolved this by installing from github instead of PyPI as suggested in the github issue:

pip install git+https://github.com/mitsuhiko/[email protected]
like image 96
Andrew Skirrow Avatar answered Nov 10 '22 14:11

Andrew Skirrow


I am using Python 3.4 on Windows and I got the same exact error.

What I did to solve the problem was uninstalling the flask babel module as follows:

flask\Scripts\pip uninstall flask-babel

The babel module is still imported correctly after that:

import babel

The only problem is I don't understand the rationale behind this.

like image 22
multigoodverse Avatar answered Nov 10 '22 16:11

multigoodverse