1

I created the following simple Flask app. I get ImportError: No module named Utils when I try to run it. I have a file called email.py, if I rename it, the error goes away. python --version returns 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
davidism
  • 121,510
  • 29
  • 395
  • 339
gpguo
  • 11
  • 1
  • 4
  • 4
    It might be because there is an `email` class in the flask module that is used in the construction of the app. If your app script has the same name as that, then Python will be confused. I'm just going off the last line of your traceback, `from email.Utils import parsedate_t` – small_data88 Jul 19 '15 at 16:19
  • yes,when I rename it ,it goes away – gpguo Jul 20 '15 at 00:16
  • I meant to to say, email package and Utils module, not email class, but I hope everything worked out for you with the renaming of your script. – small_data88 Jul 20 '15 at 14:09

2 Answers2

2

Okay, so it looks like that there was a naming conflict with your script named email.py. There is a package in Python called email with module 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. Just rename your custom script as email1.py, Email.py, or anything other than email.py and you should be okay.

small_data88
  • 380
  • 1
  • 10
0

I put a socket.py file at the workspace, and same problem as yours occurred. Change the filename to prevent the module conflict should fix this.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320