提问人:Michael Sohnen 提问时间:7/23/2022 最后编辑:Michael Sohnen 更新时间:7/23/2022 访问量:251
Python 跟踪模块中的异常:“Charmap 无法编码...”
Exception in Python Trace Module: "Charmap Can't Encode..."
问:
我正在使用 python 跟踪模块来弄清楚为什么我的程序提前退出。
但是,跟踪模块本身似乎存在错误。
当我在powershell中运行时,出现以下错误:py -m trace -t src/main.py > temp/trace.txt
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>
有没有直接的方法可以解决这个问题?
答:
3赞
Mark Ransom
7/23/2022
#1
问题在于 trace 模块正在打开一个具有 Python 实现默认编码的文件。在 Windows 上,这通常是您可以在错误转储中看到的。CP1252 无法对所有可能的 Unicode 字符进行编码,因此有时您会遇到此问题。cp1252
Python 将使用环境变量来覆盖默认值。如果在启动 Python 之前将其设置为可以对所有 Unicode 字符进行编码的内容,例如 或 它将消除错误。不幸的是,这将影响您的程序打开的所有其他文本文件。PYTHONIOENCODING
utf-8
utf-8-sig
评论