17

I've got my Pylint install importing flask just fine. And with that same installation of flask, I have wtforms running just fine in my application. However, when I run Pylint on a file importing wtforms:

from flask.ext import wtf
from flask.ext.wtf import validators

class PostForm(wtf.Form):
    content = wtf.TextAreaField('Content', validators=[validators.Required()])

From Pylint I get:

E:  1,0: No name 'wtf' in module 'flask.ext'
E:  2,0: No name 'wtf' in module 'flask.ext'
F:  2,0: Unable to import 'flask.ext.wtf'

While searching around I found this discussion suggesting it may be because flask.ext libraries are actually just "shortcuts" to libraries.

Any idea of how I can fix this? Thank you much!

4

3 回答 3

26

So flask.ext is actually a custom importer written by Armin in an awesome way. It allows people install extensions to flask in separate packages but import them in one consistent way. (Really you should go read the code for it. It is fantastic.) That said, apparently pylint doesn't appreciate the elegance (and this is actually a bug, in my opinion) but you're in luck. The easy way around this is to do the following

import flask_wtf as wtf
# The above is the equivalent line as:
# from flask.ext import wtf
from flask_wtf import validators
# This is the same as:
# from flask.ext.wtf import validators

This should make pylint happy. It isn't as nice as using flask.ext but you have to pick shutting up pylint or using elegant code, but you can't have both (right now).

于 2013-04-17T14:41:06.460 回答
7

Having been annoyed by this bug for a while, I created a pylint plugin to solve this issue. Code is at https://github.com/jschaf/pylint-flask

To enable pylint to 'see' the flask.ext modules do the following:

  1. pip install pylint-flask
  2. run pylint --load-plugins=pylint_flask <your module>
于 2015-06-28T22:17:33.027 回答
0

Use flask_<> instead of flask.ext.<>

Instead of "from flask.ext.wtf import validators" use "from flask_wtf import validators"

Faced the same problem for Mail and Message module. Had to use "from flask_mail import Message, Mail" instead of "from flask.ext.mail import Message, Mail"

于 2016-12-07T11:54:50.217 回答