Forums

ImportError: No module named 'flask_wtf'

Whats going on? I have wtf forms in my requirements.txt

wtforms==1.0.5
flask-wtf==0.9.3


Traceback (most recent call last):
2014-02-12 13:51:08,228 :  File "/bin/user_wsgi_wrapper.py", line 67, in __call__
2014-02-12 13:51:08,228 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-02-12 13:51:08,229 :  File "/usr/lib/python3.3/logging/__init__.py", line 1269, in exception
2014-02-12 13:51:08,229 :    self.error(msg, *args, **kwargs)
2014-02-12 13:51:08,229 :  File "/usr/lib/python3.3/logging/__init__.py", line 1262, in error
2014-02-12 13:51:08,230 :    self._log(ERROR, msg, args, **kwargs)
2014-02-12 13:51:08,230 :  File "/usr/lib/python3.3/logging/__init__.py", line 1368, in _log
2014-02-12 13:51:08,231 :    self.handle(record)
2014-02-12 13:51:08,231 :  File "/usr/lib/python3.3/logging/__init__.py", line 1377, in handle
2014-02-12 13:51:08,232 :    if (not self.disabled) and self.filter(record):
2014-02-12 13:51:08,232 :  File "/usr/lib/python3.3/logging/__init__.py", line 687, in filter
2014-02-12 13:51:08,232 :    for f in self.filters:
2014-02-12 13:51:08,232 :  File "/bin/user_wsgi_wrapper.py", line 59, in __call__
2014-02-12 13:51:08,233 :    app_iterator = self.app(environ, start_response)
2014-02-12 13:51:08,233 :  File "/bin/user_wsgi_wrapper.py", line 73, in import_error_application
2014-02-12 13:51:08,233 :    raise e
2014-02-12 13:51:08,233 :  File "/bin/user_wsgi_wrapper.py", line 67, in __call__
2014-02-12 13:51:08,233 :    self.error_log_file.logger.exception("Error running WSGI application")
2014-02-12 13:51:08,233 :  File "/usr/lib/python3.3/logging/__init__.py", line 1269, in exception
2014-02-12 13:51:08,234 :    self.error(msg, *args, **kwargs)
2014-02-12 13:51:08,234 :  File "/usr/lib/python3.3/logging/__init__.py", line 1262, in error
2014-02-12 13:51:08,235 :    self._log(ERROR, msg, args, **kwargs)
2014-02-12 13:51:08,235 :  File "/usr/lib/python3.3/logging/__init__.py", line 1368, in _log
2014-02-12 13:51:08,236 :    self.handle(record)
2014-02-12 13:51:08,236 :  File "/usr/lib/python3.3/logging/__init__.py", line 1377, in handle
2014-02-12 13:51:08,237 :    if (not self.disabled) and self.filter(record):
2014-02-12 13:51:08,237 :  File "/usr/lib/python3.3/logging/__init__.py", line 687, in filter
2014-02-12 13:51:08,238 :    for f in self.filters:
2014-02-12 13:51:08,238 :  File "/bin/user_wsgi_wrapper.py", line 59, in __call__
2014-02-12 13:51:08,238 :    app_iterator = self.app(environ, start_response)
2014-02-12 13:51:08,238 :  File "/bin/user_wsgi_wrapper.py", line 73, in import_error_application
2014-02-12 13:51:08,238 :    raise e
2014-02-12 13:51:08,238 :  File "/bin/user_wsgi_wrapper.py", line 82, in <module>
2014-02-12 13:51:08,239 :    application = load_wsgi_application()
2014-02-12 13:51:08,239 :  File "/bin/user_wsgi_wrapper.py", line 78, in load_wsgi_application
2014-02-12 13:51:08,239 :    return __import__(os.environ['WSGI_MODULE'], globals(), locals(), ['application']).application
2014-02-12 13:51:08,239 :  File "/var/www/rcham_pythonanywhere_com_wsgi.py", line 16, in <module>
2014-02-12 13:51:10,418 :    from addressbook.app import app as application
2014-02-12 13:51:10,418 :  File "/home/rcham/mysite/addressbook/app.py", line 4, in <module>
2014-02-12 13:51:10,486 :    from addressbook.forms import LoginForm, ContactsForm
2014-02-12 13:51:10,486 :  File "/home/rcham/mysite/addressbook/forms.py", line 1, in <module>
2014-02-12 13:51:10,491 :    from flask_wtf import Form
2014-02-12 13:51:10,491 :ImportError: No module named 'flask_wtf'

[edited by admin: formatting]

How are you installing the requirements? PythonAnywhere doesn't automatically pick up requirements from a requirements.txt, so if there's anything in there that isn't listed on our system packages for the version of Python that you're using then you'll need to install it manually. It looks like your web app is using Python 3, and we don't have flask-wtf installed by default for that Python version.

Here's the command line to install from your requirements file (you'll need to run it from a Bash console):

pip install --user -r requirements.txt

Hi, I did that but still get the same error :( I installed it manually and still no luck :(

I tried changing it to flask.ext.wtf instead of flask_wtf but no luck. Although slightly different error:

2014-02-12 15:39:59,905 :  File "/usr/local/lib/python3.3/dist-packages/flask/exthook.py", line 87, in load_module
2014-02-12 15:39:59,905 :    raise ImportError('No module named %s' % fullname)
2014-02-12 15:39:59,905 :ImportError: No module named flask.ext.wtf

[edited by admin: formatting]

Would you mind if I took a look at your files? That might make it easer to track the problem down.

Also, did you get any errors from the pip install command?

sure go ahead, no errors that I can see

Hmm, I don't see any locally-installed packages in your file storage space. Did you miss out the --user flag on that pip install?

I did a pip install --user -U Flask-WTF just now. Where do you look to find it?

I see it put in /home/rcham/.local/lib/python2.7/site-packages

Does it make a difference whether its 2.7 or 3.3 in this case?

That's right, it should appear in /home/rcham/.local/. I don't think that directory was there when I looked earlier, but it's definitely there now.

Good point about the version; it does matter -- I should have said to use this command line earlier:

pip-3.3 install --user -r requirements.txt

Sorry that is a stupid question Im sure it makes a difference. But why did it choose python2.7 when I run in bash?

It's not a stupid question at all :-) Python 2.7 is the default on PythonAnywhere -- it's still the most popular version generally, because a lot of useful modules haven't been ported to it. So if you don't specify a version, then you'll get 2.7.

Yes!!! Its working now, hmm got some other issues but this one has bugged me all day. Thanks!!

Excellent, glad it's working! Thanks for letting us know.

Even after changing different import styles and re-installing flask, flask-wtf, if it still does not works : then in the config.py("config.py may be of different name eg app_config.py etc)

insert the line

sys.path.insert(0, os.path.join(os.path.dirname(file), 'venv/Lib/site-packages'))

set the lib path of your own app , in my case it was "venv/Lib/site-packages"

@moovon -- I think you might have a different problem. Check out our guide to using virtualenvs if that's what you want to do.