How to solve "No module named 'cStringIO'" when importing the logging module in Python 3
<p>I'm trying to run the following script, named <code>msgpack_checker.py</code>, in Python 3:</p>

<pre><code>import msgpack
from faker import Faker
import logging
from logging.handlers import RotatingFileHandler

fake = Faker()
fake.seed(0)

data_file = "my_log.log"

logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler(data_file, maxBytes=2000, backupCount=10)
handler.terminator = ""         # Suppress the newline character (only works in Python 3)
logger.addHandler(handler)

fake_dicts = [{'name': fake.name()} for _ in range(100)]

for item in fake_dicts:
    dump_string = msgpack.packb(item)
    # print dump_string
    logger.debug(dump_string)

unpacker = msgpack.Unpacker(open(data_file))

print("Printing unpacked contents:")
for unpacked in unpacker:
    print(unpacked)
</code></pre>

<p>when I run it with Python 2, it prints the following output:</p>

<pre><code>Printing unpacked contents:
{'name': 'Joshua Carter'}
10
{'name': 'David Williams'}
10
{'name': 'Joseph Jones'}
10
{'name': 'Gary Perry'}
10
{'name': 'Terry Wells'}
10
{'name': 'Vanessa Cooper'}
10
{'name': 'Michael Simmons'}
10
{'name': 'Nicholas Kline'}
10
{'name': 'Lori Bennett'}
10
</code></pre>

<p>The numbers "10" I believe come from the logger, and should be removed in Python 3 by the <code>handler.terminator = ""</code> command. However, if I try to run the script using <code>python3 msgpack_checker.py</code>, I get the following error:</p>

<pre><code>Traceback (most recent call last):
  File "msgpack_checker.py", line 3, in &lt;module&gt;
    import logging
  File "/home/kurt/Documents/Scratch/logging/__init__.py", line 26, in &lt;module&gt;
    import sys, os, time, cStringIO, traceback, warnings, weakref
ImportError: No module named 'cStringIO'
</code></pre>

<p>Apparently the <code>logging</code> module tries to import <code>cStringIO</code> directly, which no longer exists in Python 3. I've seen fixes which involve importing <code>StringIO</code> from <code>io</code> instead of <code>StringIO</code>, but I not sure they would work here. Any suggestions on how to get this script to work in Python 3?</p>

<p>As pointed out in several comments, I accidentally left a directory <code>logging</code> in the same directory which is what the error message refers to. After removing that directory, I get a different error message,</p>

<pre><code>Printing unpacked contents:
Traceback (most recent call last):
  File "msgpack_checker.py", line 27, in &lt;module&gt;
    for unpacked in unpacker:
  File "msgpack/_unpacker.pyx", line 459, in msgpack._unpacker.Unpacker.__next__ (msgpack/_unpacker.cpp:459)
  File "msgpack/_unpacker.pyx", line 380, in msgpack._unpacker.Unpacker._unpack (msgpack/_unpacker.cpp:380)
  File "msgpack/_unpacker.pyx", line 370, in msgpack._unpacker.Unpacker.read_from_file (msgpack/_unpacker.cpp:370)
TypeError: expected bytes, str found
</code></pre>

<p>but that is a separate issue; at least the importing of <code>logging</code> was successful.</p>

python python-3.x