Exception in Python Trace Module: "Charmap Can't Encode..." - python-3.x

I am using the python trace module in order to figure out why my program is exiting early.
However, there seems to be a bug with the trace module itself.
When i run py -m trace -t src/main.py > temp/trace.txt in powershell, I get the following error:
Traceback (most recent call last):
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\trace.py", line 740, in <module>
main()
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\trace.py", line 728, in main
t.runctx(code, globs, globs)
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\trace.py", line 450, in runctx
exec(cmd, globals, locals)
File "src/main.py", line 11, in <module>
from project_core.image_processing import *
File "G:\My Drive\Swamynathan Lab Image Processing\ImageProcessingProjectLatestCodeWithGitVersionTracking\project\project_core\image_processing.py", line 7, in <module>
import numpy as np
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\__init__.py", line 154, in <module>
from . import polynomial
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\polynomial\__init__.py", line 116, in <module>
from .polynomial import Polynomial
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\polynomial\polynomial.py", line 87, in <module>
from ._polybase import ABCPolyBase
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\polynomial\_polybase.py", line 18, in <module>
class ABCPolyBase(abc.ABC):
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\polynomial\_polybase.py", line 73, in ABCPolyBase
"0": "⁰",
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\polynomial\_polybase.py", line 73, in ABCPolyBase
"0": "⁰",
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\trace.py", line 575, in localtrace_trace
print("%s(%d): %s" % (bname, lineno,
File "C:\Users\micha\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2070' in position 32: character maps to <undefined>
Is there any straightforward way to fix this problem?

The problem is that the trace module is opening a file with the default encoding for your Python implementation. On Windows this is often cp1252 which you can see in the error dump. CP1252 is incapable of encoding every possible Unicode character, so sometimes you'll run into this problem.
Python will use the environment variable PYTHONIOENCODING to override the default. If you set it before you start Python to something that can encode all the Unicode characters such as utf-8 or utf-8-sig it will eliminate the error. Unfortunately this will affect all other text files opened by your program.

Related

Python 3.11. Issue

I've installed fresh version of Python 3.11. on Win 10 x64 and figured I should try Jupyter notebook, but after installing it using pip command (without any kind of error and previously adding python to environment variable path) I've received this error:
`
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Scripts\jupyter-notebook.EXE\__main__.py", line 4, in <module>
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\site-packages\notebook\notebookapp.py", line 59, in <module>
from tornado import httpserver
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\site-packages\tornado\httpserver.py", line 32, in <module>
from tornado.http1connection import HTTP1ServerConnection, HTTP1ConnectionParameters
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\site-packages\tornado\http1connection.py", line 34, in <module>
from tornado import iostream
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\site-packages\tornado\iostream.py", line 40, in <module>
from tornado.netutil import ssl_wrap_socket, _client_ssl_defaults, _server_ssl_defaults
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\site-packages\tornado\netutil.py", line 35, in <module>
_client_ssl_defaults = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 775, in create_default_context
context.load_default_certs(purpose)
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 596, in load_default_certs
self._load_windows_store_certs(storename, purpose)
File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 588, in _load_windows_store_certs
self.load_verify_locations(cadata=certs)
ssl.SSLError: [ASN1] nested asn1 error (_ssl.c:4004)
`
searching stack overflow as wheel as gitHub resulted in many dead ends.
I would be very grateful for any kind of assistance.

Python, pyinstaller raised an error as follows

when I ran the application, it raised error as follows.
My python version is 3.8.10 and pyinstaller version is 4.4.
How to deal with it?
Traceback (most recent call last):
File "run.py", line 360, in huaban.run.MainWorkPanel.start_download
File "run.py", line 594, in huaban.run.MainWorkPanel.real_run
File "scrapy\crawler.py", line 280, in __init__
File "scrapy\crawler.py", line 152, in __init__
File "scrapy\crawler.py", line 146, in _get_spider_loader
File "scrapy\spiderloader.py", line 67, in from_settings
File "scrapy\spiderloader.py", line 24, in __init__
File "scrapy\spiderloader.py", line 51, in _load_all_spiders
File "scrapy\utils\misc.py", line 83, in walk_modules
File "C:\soft\VirtualenvFiles\huaban\Lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_pkgutil.py", line 71, in _pyi_pkgutil_iter_modules
assert pkg_path.startswith(SYS_PREFIX)
TypeError: startswith first arg must be str or a tuple of str, not PureWindowsPath
Fixed in PyInstaller 4.5.
See here.

spyder unicode decode error in startup

I was using spyder-ide while parsing a tumblr page with the permission of the author, and at some point everything just crashed. Even my linux system had freezed. Well, to cut to the chase now I can not start spyder, it gives me the following error after I had written spyder to my terminal:
Traceback (most recent call last):
File "/home/dk/anaconda3/bin/spyder", line 2, in <module>
from spyderlib import start_app
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/start_app.py", line 13, in <module>
from spyderlib.config import CONF
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/config.py", line 736, in <module>
subfolder=SUBFOLDER, backup=True, raw_mode=True)
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 215, in __init__
self.load_from_ini()
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 265, in load_from_ini
self.read(self.filename(), encoding='utf-8')
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 696, in read
self._read(fp, filename)
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 1012, in _read
for lineno, line in enumerate(fp, start=1):
File "/home/dk/anaconda3/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: invalid continuation byte
I tried the solution here and I had received the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/spyder.py", line 107, in <module>
from spyderlib.utils.qthelpers import qapplication
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/utils/qthelpers.py", line 24, in <module>
from spyderlib.guiconfig import get_shortcut
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/guiconfig.py", line 22, in <module>
from spyderlib.config import CONF
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/config.py", line 736, in <module>
subfolder=SUBFOLDER, backup=True, raw_mode=True)
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 215, in __init__
self.load_from_ini()
File "/home/dk/anaconda3/lib/python3.5/site-packages/spyderlib/userconfig.py", line 265, in load_from_ini
self.read(self.filename(), encoding='utf-8')
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 696, in read
self._read(fp, filename)
File "/home/dk/anaconda3/lib/python3.5/configparser.py", line 1012, in _read
for lineno, line in enumerate(fp, start=1):
File "/home/dk/anaconda3/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 0-1: invalid continuation byte
I tried uninstalling and reinstalling anaconda and it doesn't seem to work I am open to suggestions, I am very much new to python, so I would appriciate a simple explanation of the possible causes of the error too.
Thanks in advance
Well here is how I solved the issue.
l opened this: spyderlib/userconfig.py
and changed this: self.read(self.filename(), encoding='utf-8')
to this: self.read(self.filename(), encoding='latin-1')
It gave me a Warning: File contains no section headers but started spyder anyway. After that, I closed spyder, opened the terminal and entered spyder --reset then restarted spyder, it seems to work now.
Here is what you should not do at all costs for this problem: thinkering with these, I learned my lesson the hard way:
python3.5/configparser.py
python3.5/codecs.py

pyramid pserve pviews P* re.compile syntax error

I used to launch the command pserve --reload development.ini in my vagrant box to launch a pyramid server locally. The command was working until the last few days.
However, I now have a syntax error preventing me from launching pserve with or without conf file.
(py32)vagrant.vm bin # pserve
Traceback (most recent call last):
File "/var/virtualenv/py32/bin/pserve", line 9, in <module>
load_entry_point('pyramid==1.5.1', 'console_scripts', 'pserve')()
File "/var/virtualenv/py32/lib/python3.2/site-packages/pkg_resources/__init__.py", line 519, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pkg_resources/__init__.py", line 2630, in load_entry_point
return ep.load()
File "/var/virtualenv/py32/lib/python3.2/site-packages/pkg_resources/__init__.py", line 2310, in load
return self.resolve()
File "/var/virtualenv/py32/lib/python3.2/site-packages/pkg_resources/__init__.py", line 2316, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/scripts/pserve.py", line 31, in <module>
from pyramid.paster import setup_logging
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/paster.py", line 10, in <module>
from pyramid.scripting import prepare
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/scripting.py", line 1, in <module>
from pyramid.config import global_registries
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/config/__init__.py", line 9, in <module>
from webob.exc import WSGIHTTPException as WebobWSGIHTTPException
File "/var/virtualenv/py32/lib/python3.2/site-packages/webob/__init__.py", line 2, in <module>
from webob.request import *
File "/var/virtualenv/py32/lib/python3.2/site-packages/webob/request.py", line 10, in <module>
import simplejson as json
File "/var/virtualenv/py32/lib/python3.2/site-packages/simplejson/__init__.py", line 113, in <module>
from .encoder import JSONEncoder, JSONEncoderForHTML
File "/var/virtualenv/py32/lib/python3.2/site-packages/simplejson/encoder.py", line 22
ESCAPE = re.compile(u'[\\x00-\\x1f\\\\"\\b\\f\\n\\r\\t\u2028\u2029]')
^
SyntaxError: invalid syntax
I use python 3.2.3 in this virtualenv
pviews and proutes give the same error
Any help appreciated :)
Regards
==========================
Thanks to Steve Piercy I uninstalled simplejson and pserve now works launched alone! Thanks again.
However, when I launch pserve development.ini, I now have another error:
(py32)vagrant.vm app # pserve --reload development.ini
Starting subprocess with file monitor
Traceback (most recent call last):
File "/var/virtualenv/py32/bin/pserve", line 9, in <module>
load_entry_point('pyramid==1.5.1', 'console_scripts', 'pserve')()
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/scripts/pserve.py", line 51, in main
return command.run()
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/scripts/pserve.py", line 316, in run
global_conf=vars)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/scripts/pserve.py", line 340, in loadapp
return loadapp(app_spec, name=name, relative_to=relative_to, **kw)
File "/var/virtualenv/py32/lib/python3.2/site-packages/paste/deploy/loadwsgi.py", line 247, in loadapp
return loadobj(APP, uri, name=name, **kw)
File "/var/virtualenv/py32/lib/python3.2/site-packages/paste/deploy/loadwsgi.py", line 272, in loadobj
return context.create()
File "/var/virtualenv/py32/lib/python3.2/site-packages/paste/deploy/loadwsgi.py", line 710, in create
return self.object_type.invoke(self)
File "/var/virtualenv/py32/lib/python3.2/site-packages/paste/deploy/loadwsgi.py", line 146, in invoke
return fix_call(context.object, context.global_conf, **context.local_conf)
File "/var/virtualenv/py32/lib/python3.2/site-packages/paste/deploy/util.py", line 56, in fix_call
val = callable(*args, **kw)
File "/var/www/app/app/__init__.py", line 63, in main
request_factory=RequestFactory,
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/config/__init__.py", line 301, in __init__
exceptionresponse_view=exceptionresponse_view,
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/config/__init__.py", line 412, in setup_registry
self.include(inc)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid/config/__init__.py", line 755, in include
c(configurator)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid_jinja2/__init__.py", line 468, in includeme
_get_or_build_default_environment(config.registry)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid_jinja2/__init__.py", line 309, in _get_or_build_default_environment
filters = parse_config(settings.get('jinja2.filters', ''))
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid_jinja2/__init__.py", line 62, in parse_config
result[name.strip()] = maybe_import_string(impl)
File "/var/virtualenv/py32/lib/python3.2/site-packages/pyramid_jinja2/__init__.py", line 44, in maybe_import_string
return import_string(val.strip())
File "/var/virtualenv/py32/lib/python3.2/site-packages/jinja2/utils.py", line 213, in import_string
return getattr(__import__(module, None, None, [obj]), obj)
File "/var/www/app/app/views/filters.py", line 1, in <module>
from babel.dates import format_datetime, get_timezone, format_timedelta, format_date, UTC
File "/usr/lib/python3.2/importlib/_bootstrap.py", line 436, in load_module
return self._load_module(fullname)
File "/usr/lib/python3.2/importlib/_bootstrap.py", line 141, in decorated
return fxn(self, module, *args, **kwargs)
File "/usr/lib/python3.2/importlib/_bootstrap.py", line 342, in _load_module
exec(code_object, module.__dict__)
File "/tmp/pip-build-z2s2e9/babel/babel/__init__.py", line 20, in <module>
File "/var/virtualenv/py32/lib/python3.2/site-packages/babel/core.py", line 394
retval += ' (%s)' % u', '.join(details)
^
SyntaxError: invalid syntax
I tried to uninstall-reinstall babel with no luck.
Thanks for your help!
Did you recently install simplejson? It was and never will be compatible with Python 3.0 - 3.2. See https://github.com/simplejson/simplejson/issues/66#issuecomment-15360824

Sphinx throws an exception on Python 3.4/Windows

I have successfully installed the sphnix on Windows/Python 3.4, then successfully generated a basic structure by calling sphinx-quickstart.
But when calling sphinx-build html or make html, it throws an exception:
D:\my-doc>make html
Traceback (most recent call last):
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main "__main__", mod_spec)
File "C:\Python34\lib\runpy.py", line 85, in _run_code exec(code, run_globals)
File "C:\Python34\Scripts\sphinx-build.exe\__main__.py", line 9, in <module>
File "C:\Python34\lib\site-packages\sphinx\__init__.py", line 51, in main sys.exit(build_main(argv))
File "C:\Python34\lib\site-packages\sphinx\__init__.py", line 61, in build_main from sphinx import cmdline
File "C:\Python34\lib\site-packages\sphinx\cmdline.py", line 23, in <module> from sphinx.application import Sphinx
File "C:\Python34\lib\site-packages\sphinx\application.py", line 37, in <module> from sphinx.builders import BUILTIN_BUILDERS
File "C:\Python34\lib\site-packages\sphinx\builders\__init__.py", line 23, in <module> from sphinx.util import i18n, path_stabilize
File "C:\Python34\lib\site-packages\sphinx\util\i18n.py", line 15, in <module> from babel.messages.pofile import read_po
File "C:\Python34\lib\site-packages\babel\messages\__init__.py", line 12, in <module> from babel.messages.catalog import *
File "C:\Python34\lib\site-packages\babel\messages\catalog.py", line 23, in <module> from babel.dates import format_datetime
File "C:\Python34\lib\site-packages\babel\dates.py", line 28, in <module> from babel.util import UTC, LOCALTZ
File "C:\Python34\lib\site-packages\babel\util.py", line 278, in <module> from babel import localtime
File "C:\Python34\lib\site-packages\babel\localtime\__init__.py", line 21, in <module> from babel.localtime._win32 import _get_localzone
File "C:\Python34\lib\site-packages\babel\localtime\_win32.py", line 18, in <module> tz_names = get_global('windows_zone_mapping')
File "C:\Python34\lib\site-packages\babel\core.py", line 58, in get_global _global_data = pickle.load(fileobj) TypeError: an integer is required (got type str)
I guess this is about compatibilities, since I have followed the tutorial step by step. Also been googling for this issue for hours and could not find an answer so far. Is it something I'm doing wrong or is it a compatibility issue?
The bug appears to be in babel v2.0. I downgraded to 1.3 (pip install babel==1.3) and the issue went away. Let me know if this works for you.

Resources