2

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.

davidism
  • 121,510
  • 29
  • 395
  • 339
chmike
  • 20,922
  • 21
  • 83
  • 106
  • sorry, but have you installed babel package for that environment? virtualenv now uses the --no-site-packages flag default – jxs Dec 22 '12 at 17:18
  • No. After activating the virtualenv, trying to install all packages return "Requirement already satisfied". Nothing is apparently changed in the virtualenv. – chmike Dec 22 '12 at 17:35
  • In the shell with the activated virtualenv, if in the python shell I import sqlalchemy there is no problem, as well as for Flask. The problem is only for the Flask extension packages. – chmike Dec 22 '12 at 17:38
  • I tried creating the vitrualenv with the command "virtualenv --system-site-packages venv" but I get the same error message. – chmike Dec 22 '12 at 17:46
  • i don't really know whats happening , but if you add the $PYTONPATH env var to your bash profile, and get the global packages in each environment i think it solves the problem : export PYTHONPATH="/usr/local/lib/python2.7/site-packages" for example – jxs Dec 22 '12 at 17:51
  • The PYTHONPATH env variable is there and includes the path to the site-packages. I'm using Windows 7 64bit. The problem is really with the the Flask extension lookup system. – chmike Dec 22 '12 at 18:02

4 Answers4

2

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.

Audrius Kažukauskas
  • 13,173
  • 4
  • 53
  • 54
  • I tried both and none worked. Looking at the Flask code comments I saw that it will try all three possible import namings in sequence until one succeed. I must make sure I tested the namings with the correct virtualenv parametrr. – chmike Dec 24 '12 at 10:22
  • I just tried it with a virtual environment created with "virtualenv --system-site-packages venv" and from flask.ext.babel import Babel got the message File "c:\Python27\Lib\site-packages\flask\exthook.py", line 86, in load_module raise ImportError('No module named %s' % fullname) ImportError: No module named flask.ext.babel. So the extension processing code of Flask is called but the extension can't be found. – chmike Dec 24 '12 at 12:32
  • In virtualenv's Python shell try `import sys; sys.path` and see if it really points to system-wide site-packages directory. Although I'd strongly recommend to use isolated virtualenv and install everything in it. That's what I do in my Linux systems, and the only packages that go into system site-packages are those installed via my distribution's package manager. You'll immediately see the benefits when working on multiple projects which depend on different versions of the same libraries. – Audrius Kažukauskas Dec 24 '12 at 14:28
  • I understand the benefit of virtualenv. What I don't understand is why it accept to import any globally installed package (what it should) (e.g Babel,Flask) and fails to accept importing the flask_babel package (e.g. flaskext.babel). In the global directory the file Lib/site-packages/flaskext/babel.py exits. It should find it like any other modules. – chmike Dec 24 '12 at 15:02
  • Check my answer that solved the problem by adding an empty __init__.py in the flaskext folder. Thank you very much for helping me. It led me to the solution. – chmike Dec 24 '12 at 15:28
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.

chmike
  • 20,922
  • 21
  • 83
  • 106
1

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.

Community
  • 1
  • 1
Markus Unterwaditzer
  • 7,992
  • 32
  • 60
  • The form *import flask_babel* doesn't work in the global and local environment. The comments in the flask.exthook.py tells otherwise. Your suggestion should work, but unfortunately doesn't. Thank you very much for helping me solving the problem. – chmike Dec 24 '12 at 15:27
0

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

kinsley kajiva
  • 1,840
  • 1
  • 21
  • 26