Python / flask: TypeError: integer required (str type obtained)

I am new to python and flask. I am trying to make my way through the flask tutorial at http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world . I encountered an error that I canโ€™t understand. I have exhaustively (I think) searched for a solution, but I cannot find one that suits my problem. I removed and reinstalled the flask. I even created a new venv and started all over again, but nothing worked.

I am trying to create a simple web form application. 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 an error

 TypeError: an integer is required (got type str) 

Full stack: 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 - it drives me crazy!

+6
source share
4 answers

This is a babel 2.0 package build error in python package index

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

To summarize, a disguised babel/global.dat included in the package and this cannot be read by python 3 because it was created by a script running python 2.

I solved this by installing from github instead of PyPI, as suggested in the github problem:

 pip install git+https://github.com/mitsuhiko/ babel.git@2.0 
+5
source

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

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

 flask\Scripts\pip uninstall flask-babel 

The babel module is still imported correctly:

 import babel 

The only problem is that I do not understand the reasons for this.

+1
source

I got this same problem at about the same time as you - I was very excited to see the stackoverflow page on it to see it unanswered. I seem to have found a fix, but I cautioned him against the fact that I am by no means an expert and tomorrow I discover that it has broken something else.

Rooted in core.py dependencies, I found that depending on the version of python you are running, / babel / _compat.py imports either cPickle or pickle, and what core.py imports as pickle.

I changed line 22 in / babel / _compat.py from

 import cPickle as pickle 

to

 import pickle 

and thatโ€™s all working.

Again, this can break a lot of things that I donโ€™t know about, but from what I read, it seems that cPickle is faster, so it just sacrifices speed and should not change functionality. If you just follow the flask guide, I doubt it will be a problem.

0
source

My version of / babel / _compat.py already had import pickle , so I decided to try uninstalling and reinstalling the flash drive. After I deleted it, I started the application only out of curiosity - and it worked! I'm sorry that I did not understand why, but now I am unlocked.

0
source

All Articles