Reputation: 57601
I'm trying to run the following script, named msgpack_checker.py
, in Python 3:
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)
when I run it with Python 2, it prints the following output:
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
The numbers "10" I believe come from the logger, and should be removed in Python 3 by the handler.terminator = ""
command. However, if I try to run the script using python3 msgpack_checker.py
, I get the following error:
Traceback (most recent call last):
File "msgpack_checker.py", line 3, in <module>
import logging
File "/home/kurt/Documents/Scratch/logging/__init__.py", line 26, in <module>
import sys, os, time, cStringIO, traceback, warnings, weakref
ImportError: No module named 'cStringIO'
Apparently the logging
module tries to import cStringIO
directly, which no longer exists in Python 3. I've seen fixes which involve importing StringIO
from io
instead of StringIO
, but I not sure they would work here. Any suggestions on how to get this script to work in Python 3?
Upvotes: 1
Views: 3149
Reputation: 57601
As pointed out in several comments, I accidentally left a directory logging
in the same directory which is what the error message refers to. After removing that directory, I get a different error message,
Printing unpacked contents:
Traceback (most recent call last):
File "msgpack_checker.py", line 27, in <module>
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
but that is a separate issue; at least the importing of logging
was successful.
Upvotes: 1