duma-repo/ai_code_reader

UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 29: illegal multibyte sequence

Closed this issue · 1 comments

D:\PycharmProjects\ai_code_reader\venv\Scripts\python.exe main.py
Traceback (most recent call last):
File "D:\PycharmProjects\ai_code_reader\main.py", line 113, in
config.init_config()
File "D:\PycharmProjects\ai_code_reader\config.py", line 18, in init_config
config.read('.env')
File "C:\Program Files\Python310\lib\configparser.py", line 698, in read
self._read(fp, filename)
File "C:\Program Files\Python310\lib\configparser.py", line 1021, in _read
for lineno, line in enumerate(fp, start=1):
UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 29: illegal multibyte sequence

Process finished with exit code 1

问题的根本原因是 .env 文件的编码格式与 Python 读取文件时使用的默认编码格式不匹配。在这种情况下,Python 默认使用 gbk 编码读取文件,但 .env 文件可能是用 utf-8 编码保存的。我们需要明确告诉 Python 使用 utf-8 编码来读取 .env 文件。

可以通过在 config.read() 方法中指定编码来解决这个问题:

def init_config():
    # 创建一个配置解析器对象
    config = configparser.ConfigParser()
    with open('.env', 'r', encoding='utf-8') as f:
        config.read_file(f)

    # 项目目录
    os.environ['PRJ_DIR'] = config.get('prj', 'dir')
    if not os.environ['PRJ_DIR']:
        raise ValueError('没有设置项目路径')

    # 配置 openai 环境变量
    os.environ['OPENAI_BASE_URL'] = config.get('openai', 'base_url')
    os.environ['OPENAI_API_KEY'] = config.get('openai', 'api_key')

    # 设置代理
    http_proxy = config.get('openai', 'http_proxy')
    https_proxy = config.get('openai', 'https_proxy')
    if http_proxy:
        os.environ['http_proxy'] = http_proxy
    if https_proxy:
        os.environ['https_proxy'] = https_proxy

    # 配置本地大模型,魔搭环境变量
    modelscope_cache = config.get('local_llm', 'modelscope_cache')
    if modelscope_cache:
        os.environ['MODELSCOPE_CACHE'] = modelscope_cache