Python3 StringIO has no clone - python-3.x

Following code works in Python2 but not in Python3?
import http.client
from io import StringIO
if __name__ == '__main__':
res=http.client.HTTPMessage(StringIO(u"headers"))
print(str(res))
[]$ python3 test.py
Traceback (most recent call last):
File "test.py", line 9, in <module>
print(str(res))
File "/usr/lib64/python3.9/email/message.py", line 135, in __str__
return self.as_string()
File "/usr/lib64/python3.9/email/message.py", line 158, in as_string
g.flatten(self, unixfrom=unixfrom)
File "/usr/lib64/python3.9/email/generator.py", line 97, in flatten
policy = policy.clone(max_line_length=self.maxheaderlen)
AttributeError: '_io.StringIO' object has no attribute 'clone'
I'm currently porting old Python2 code over to Python3.
This is a dummy test of a problem I run into.

Related

facing difficulty after running chat in python

Traceback (most recent call last):
File "F:\test\AI_ChatBot_Python-master\new.py", line 2, in
chatbot = ChatBot(
File "C:\Users\atulk\anaconda3\lib\site-packages\chatterbot\chatterbot.py", line 34, in init
self.storage = utils.initialize_class(storage_adapter, **kwargs)
File "C:\Users\atulk\anaconda3\lib\site-packages\chatterbot\utils.py", line 54, in initialize_class
return Class(*args, **kwargs)
File "C:\Users\atulk\anaconda3\lib\site-packages\chatterbot\storage\sql_storage.py", line 22, in init
from sqlalchemy import create_engine
File "C:\Users\atulk\anaconda3\lib\site-packages\sqlalchemy_init_.py", line 8, in
from . import util as util # noqa
File "C:\Users\atulk\anaconda3\lib\site-packages\sqlalchemy\util_init.py", line 14, in
from ._collections import coerce_generator_arg # noqa
File "C:\Users\atulk\anaconda3\lib\site-packages\sqlalchemy\util_collections.py", line 16, in
from .compat import binary_types
File "C:\Users\atulk\anaconda3\lib\site-packages\sqlalchemy\util\compat.py", line 264, in
time_func = time.clock
AttributeError: module 'time' has no attribute 'clock'
Are you sure, that you're running this from Python version <3.8 because time.clock() has been removed in Python 3.8 or you can simply upgrade sqlalchemy which supports perf_counter as replacement.
Python v3.8 changes
sqlalchemy with time perf_counter
sqlalchemy with time.clock

ModuleNotFoundError: No module named '_curses' when trying to use curses in py2exe

my python code I'm trying to run:
import curses, random
def main(stdscr):
stdscr.clear()
for i in range(0, 21):
stdscr.addstr(i, 0, f'count {i+1}, and rand value {random.randint(0, 100)}')
stdscr.refresh()
stdscr.getkey()
curses.wrapper(main)
print("ngos")
then I use this for py2exe:
from distutils.core import setup
import py2exe
setup(windows=[{"script":"main.py"}], options = {"py2exe":{"packages": ["curses",]}})
but when I try and run the exe I get this from the log:
Traceback (most recent call last):
File "main.py", line 1, in <module>
File "<frozen zipimport>", line 259, in load_module
File "curses\__init__.pyc", line 13, in <module>
ModuleNotFoundError: No module named '_curses'
I'm using win10 x64

Import Image Module error while generating qrcode

I installed qrcode 7.3.1 and Image 1.5.33. Pillow 9.0.0 is also installed during Image install. But while running following example from qrcode documentation
import qrcode
img = qrcode.make('Some data here')
type(img) # qrcode.image.pil.PilImage
img.save("some_file.png")
Following error generates
Traceback (most recent call last):
File "C:\****\venv\lib\site-packages\qrcode\image\pil.py", line 5, in <module>
from PIL import Image, ImageDraw
File "C:\****\venv\lib\site-packages\PIL\Image.py", line 30, in <module>
import logging
File "C:\****\himal\anaconda3\lib\logging\__init__.py", line 26, in <module>
import sys, os, time, io, re, traceback, warnings, weakref, collections.abc
File "C:\****\anaconda3\lib\traceback.py", line 5, in <module>
import linecache
File "C:\****\anaconda3\lib\linecache.py", line 11, in <module>
import tokenize
File "c:\users\himal\anaconda3\lib\tokenize.py", line 35, in <module>
from token import EXACT_TOKEN_TYPES
ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token' (***\token.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\****\scan_qrcode.py", line 2, in <module>
img = qrcode.make('Some data here')
File "C:\****\venv\lib\site-packages\qrcode\main.py", line 13, in make
return qr.make_image()
File "C:\****\venv\lib\site-packages\qrcode\main.py", line 296, in make_image
from qrcode.image.pil import PilImage
File "C:\****\venv\lib\site-packages\qrcode\image\pil.py", line 7, in <module>
import Image
ModuleNotFoundError: No module named 'Image'
Python 3.8 is current python version.

Can't import urllib.request module in python 3

I was executing a code from the book Think Python in Python 3. The code follows.
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import urllib.request
conn = urllib.request.urlopen('http://thinkpython.com/secret.html')
for line in conn:
print(line.strip())
When I execute this code in my machine's terminal, I got this following output.
Traceback (most recent call last):
File "secret.py", line 4, in <module>
import urllib.request
File "/usr/lib/python3.6/urllib/request.py", line 84, in <module>
import base64
File "/usr/lib/python3.6/base64.py", line 10, in <module>
import struct
File "/usr/lib/python3.6/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, re, pwd, grp, os, time
File "/usr/lib/python3.6/subprocess.py", line 136, in <module>
import _posixsubprocess
ModuleNotFoundError: No module named '_posixsubprocess'
Original exception was:
Traceback (most recent call last):
File "secret.py", line 4, in <module>
import urllib.request
File "/usr/lib/python3.6/urllib/request.py", line 84, in <module>
import base64
File "/usr/lib/python3.6/base64.py", line 10, in <module>
import struct
File "/usr/lib/python3.6/struct.py", line 13, in <module>
from _struct import *
ModuleNotFoundError: No module named '_struct'
I did this same code in Anaconda, and I got the correct output. Can anybody help me with this problem.

Python Sklearn import error

I'm new to machine learning libraries in python. I've installed 'python 3.4'.
I've also installed scikit-learn , numpy and scipy from wheel files.
scikit_learn-0.18.1-cp34-none-win_amd64
scipy-0.16.0-cp34-none-win_amd64
numpy-1.9.2-cp34-none-win_amd64.whl
import scipy
import numpy
above statements are working fine. but when i try to execute import sklearn
i get below error
Traceback (most recent call last):
File "C:\Users\Usman\Desktop\hello_python.py", line 1, in <module>
import sklearn
File "C:\Python34\Lib\site-packages\sklearn\__init__.py", line 57, in <module>
from .base import clone
File "C:\Python34\Lib\site-packages\sklearn\base.py", line 12, in <module>
from .utils.fixes import signature
File "C:\Python34\Lib\site-packages\sklearn\utils\__init__.py", line 10, in <module>
from .murmurhash import murmurhash3_32
File "__init__.pxd", line 155, in init sklearn.utils.murmurhash (sklearn\utils\murmurhash.c:6319)
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 88, got 96
--edit
when i execute import sklearn from python shell i get the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\Lib\site-packages\sklearn\__init__.py", line 56, in <module>
from . import __check_build
ImportError: cannot import name '__check_build'
I've searched but couldn't find anything helpful. Kindly help me remove this error.

Resources