简体   繁体   English

错误:ImportError:无法导入名称LoginForm

[英]Error : ImportError: cannot import name LoginForm

I am facing import error while importing LoginForm 导入LoginForm时遇到导入错误

import form

This import imports perfectly. 此导入可完美导入。 But when I do 但是当我这样做

from form import LoginForm

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
from form import LoginForm
ImportError: cannot import name LoginForm

Also When I import form only and try to use LoginForm it gives me error 另外,当我仅导入表单并尝试使用LoginForm时,它给我错误

NameError: global name 'LoginForm' is not defined NameError:全局名称“ LoginForm”未定义

Please let me know the problem 请让我知道问题所在

Thanks in advance Abhishek 在此先感谢Abhishek

Do you have a file in your directory called form.py? 您的目录中是否有一个名为form.py的文件? Or is this file itself called form.py? 还是该文件本身称为form.py? If so, python could be loading that instead. 如果是这样,python可能正在加载它。 Especially if you are using 2.7,where relative imports are prioritized. 尤其是在使用2.7的情况下,相对导入优先。

Try printing 尝试列印

print(form._ file _) (Double underscore before and after file) print(form._ file _)(文件前后带有双下划线)

to see where the module is. 看看模块在哪里。 Or printing 或印刷

dir(form) 目录(窗体)

to see what is in the namespace 查看名称空间中的内容

Your import statement is fine. 您的进口对帐单很好。

If form is a file in your directory, you need to post its content. 如果form是目录中的文件,则需要发布其内容。 Most likely it's missing the definition for a LoginForm class. 很可能它缺少LoginForm类的定义。

See this tutorial from miguel to show an example: 请参阅miguel的本教程以显示示例:

http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms

Let's write our first form (file app/forms.py):

from flask.ext.wtf import Form 
from wtforms import StringField, BooleanField 
from wtforms.validators import DataRequired 

class LoginForm(Form): 
    openid = StringField('openid',  validators=[DataRequired()]) 
    remember_me = BooleanField('remember_me', default=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM