Unable to import Python's email module at all

PythonEmail

Python Problem Overview


I can't seem to import the email module at all. Every time I do it I get an error. I've tried uninstalling Python and reinstalling, but the email module just refuses to work. I've even done "pip install email" and it's still broken. I'm on Windows 7 Home Premium x64, running an x86 version of Python.

Here's what happens:

c:\Users\Nicholas\Desktop>python
ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import email
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "email.py", line 1, in <module>
    import smtplib
  File "C:\Python27\lib\smtplib.py", line 46, in <module>
    import email.utils
ImportError: No module named utils
>>>

EDIT: I've tried both Python from python.org and ActivePython, thinking ActivePython might work. Is there anyway to completely remove python and all its data and start 100% fresh maybe?

Python Solutions


Solution 1 - Python

It looks like you have a file named email.py. Don't use file names that have the same name as Python standard library modules. Generally, your working directory comes earlier on the Python search path for importing modules so files in your working directory will override modules with the same name in the standard library.

The clue: note the path names in the traceback

  File "email.py", line 1, in <module>
    import smtplib
  File "C:\Python27\lib\smtplib.py", line 46, in <module>
    import email.utils

By the way, this is a very common error. The excellent tutorial in the Python standard documentation set talks about it here.

Solution 2 - Python

I just came across this error and wanted to share my solution. In my case, I had a file named email.py in directory. This created a name conflict between Python's email.py and my file. When smtplib tried to import email.utils it looked and my file and didn't find anything. After I renamed my copy of email.py into myemail.py everything worked like a charm.

Solution 3 - Python

I also came across this error. In addition to renaming the email.py to something else, you must also remove the email.pyc (notice the C) file. After that, all is well. Thanks all!

Solution 4 - Python

I also fetched this problem because i had a file named email.py in my project directory. I wasn't able to import urllib.request . When i changed the file name email.py to emailtest.py then the error gone away. In every time we should not use the name what is same as python core file name.

Solution 5 - Python

npm install email

has fix my problem, try it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNicholasView Question on Stackoverflow
Solution 1 - PythonNed DeilyView Answer on Stackoverflow
Solution 2 - PythonVladimir BychkovskyView Answer on Stackoverflow
Solution 3 - PythonJose LeonView Answer on Stackoverflow
Solution 4 - PythonRaihanView Answer on Stackoverflow
Solution 5 - PythonSavo PušicaView Answer on Stackoverflow