简体   繁体   English

启动时Flask应用程序引发“ ImportError:没有名为Utils的模块”

[英]Flask app raises “ImportError: No module named Utils” when started

I created the following simple Flask app. 我创建了以下简单的Flask应用。 I get ImportError: No module named Utils when I try to run it. 我收到ImportError: No module named Utils尝试运行该ImportError: No module named Utils时, ImportError: No module named Utils I have a file called email.py , if I rename it, the error goes away. 我有一个名为email.py的文件,如果重命名该文件,该错误将消失。 python --version returns 2.7. python --version返回2.7。 What is causing the error? 是什么导致错误? Why does it go away when I rename the module? 为什么在重命名模块时它消失了?

from flask import Flask
from flask import request

app=Flask(__name__)

@app.route('/',methods=['GET','POST'])
def home():
return '<h1>home</h1>'
@app.route('/signin',methods=['GET'])
def signin_form():
    return '''<form action="/signin" method="post">
    <p><input name="username"></p>
    <p><input name=password"> type="password"></p>
    <p><button type="submit">Sign In</button></p>
    </form>'''

@app.route('/signin',methods=['POST'])
def signin():

   if request.form['username']=='admin' and request.form['password']=='password':
        return '<h3>hello ,admin!<h3>'
    return '<h3>bad username or password</h3>'

if __name__=='__main__':
    app.run()
Traceback (most recent call last):
  File "D:/learn/suojin.py", line 5, in <module>
    from flask import Flask
  File "C:\Python27\lib\site-packages\flask-0.10.1-py2.7.egg\flask\__init__.py", line 17, in <module>
    from werkzeug.exceptions import abort
  File "C:\Python27\lib\site-packages\werkzeug\__init__.py", line 154, in <module>
    __import__('werkzeug.exceptions')
  File "C:\Python27\lib\site-packages\werkzeug\exceptions.py", line 71, in <module>
    from werkzeug.wrappers import Response
  File "C:\Python27\lib\site-packages\werkzeug\wrappers.py", line 26, in <module>
    from werkzeug.http import HTTP_STATUS_CODES, \
  File "C:\Python27\lib\site-packages\werkzeug\http.py", line 24, in <module>
    from email.Utils import parsedate_tz
ImportError: No module named Utils

Okay, so it looks like that there was a naming conflict with your script named email.py. 好的,看起来您的名为email.py的脚本存在命名冲突。 There is a package in Python called email with module Utils . Python中有一个名为Utils 电子邮件程序包。 When Python searches for this package, it finds your script first in its path, so it grabs it and tries to import the Utils module, but cannot find it. 当Python搜索此软件包时,它将首先在其路径中找到您的脚本,因此它将对其进行捕获并尝试导入Utils模块,但找不到它。 Just rename your custom script as email1.py, Email.py, or anything other than email.py and you should be okay. 只需将自定义脚本重命名为email1.py,Email.py或email.py以外的任何其他内容,您就可以了。

I put a socket.py file at the workspace, and same problem as yours occurred. 我在工作区中放置了一个socket.py文件,并且发生了与您相同的问题。 Change the filename to prevent the module conflict should fix this. 更改文件名以防止模块冲突应解决此问题。

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

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