Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

The old way of importing Flask extension was like:

import flaskext.babel

Namespace packages were, however, "too painful for everybody involved", so now Flask extensions should be importable like:

import flask_babel

flask.ext is a special package. If you import flask.ext.babel, it will try out both of the above variants, so it should work in any case.

for python 3 install like this: pip install Flask-Babel after installing import like this :from flask.ext.babel import Babel but do note you will get the deprecation warning so you can import like this :from flask_babel import Babel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top