110

How do I solve an ImportError: No module named 'cStringIO' under Python 3.x?

smci
  • 32,567
  • 20
  • 113
  • 146
jvi
  • 1,111
  • 2
  • 7
  • 5
  • 3
    thx- accepting @SimonVissers solution. I should not have tried to install "email" it since it is available as a module. so just import email into code and make the changes to application code as needed. – jvi Jan 29 '15 at 01:22
  • 1
    This is a general issue when migrating to 3.x, and not just about installing any particular package e.g. email. – smci Apr 05 '19 at 11:58

3 Answers3

175

From Python 3.0 changelog:

The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.

From the Python 3 email documentation it can be seen that io.StringIO should be used instead:

from io import StringIO
from email.generator import Generator

fp = StringIO()
g = Generator(fp, mangle_from_=True, maxheaderlen=60)
g.flatten(msg)
text = fp.getvalue()
iacob
  • 20,084
  • 6
  • 92
  • 119
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • 5
    io module exists in my python environment. but I am unable to install email itself which was my problem. i.e. "pip install email" fails. Am I supposed to hack into some code and make changes for it to install ? – jvi Jan 29 '15 at 01:07
  • 20
    @jvi: You should not install `email`, you should only `import email` as it's part of the Python standard library. – Simeon Visser Jan 29 '15 at 01:10
16

I had the same issue because my file was called email.py. I renamed the file and the issue disappeared.

Maeda
  • 1,291
  • 15
  • 16
0

I had the issue because my directory was called email. I renamed the directory to emails and the issue was gone.

l001d
  • 723
  • 9
  • 15