Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Flask-Babel with virtualenv fails [duplicate]

Tags:

python

flask

I have Flask, Babel and Flask-Babel installed in the global packages. When running python and I type this, no error

>>> from flaskext.babel import Babel
>>>

With a virtual environment, starting python and typing the same command I see

>>> from flaskext.babel import Babel
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named flaskext.babel
>>>

The problem is that I'm using Ninja-IDE and I'm apparently forced to use a virtualenv. I don't mind as long as it doesn't break Flask packing system.

like image 427
chmike Avatar asked Jun 26 '26 21:06

chmike


2 Answers

I think you're supposed to import Flask extensions like the following from version 0.8 onwards:

from flask.ext.babel import Babel

I tried the old way (import flaskext.babel), and it didn't work for me either.

like image 110
Audrius Kažukauskas Avatar answered Jun 29 '26 10:06

Audrius Kažukauskas


Yeah! I solved the problem!

Creating an empty _init_py in the global Lib/site-packages/flaskext next to the babel.py file solves the problem.

Importing Babel from the local environment now works as expected and as it worked in the global environment.

We can use the two forms from flaskext.babel import Babel and from babel.ext.babel import Babel. However the forms *from flask_babel import Babel* or *import flask_babel* don't work.

Note that I'm running on Windows 7 64bit with Python 2.7 in C:\Python27. The absence of init.py file may not be a problem on unix computers.

like image 38
chmike Avatar answered Jun 29 '26 11:06

chmike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!