Skip to content

Conversation

@kristol07
Copy link

fix exception when logging because of encoding issue

@kristol07
Copy link
Author

windows平台下会遇到logging的exception,需要设置utf-8 encoding。

@021gink
Copy link

021gink commented Mar 10, 2025

下面的env文件读取也需要改一下utf-8 encoding:
def save_settings(self):
"""保存设置到.env文件"""
api_key = self.get_api_key()

    if not api_key:
        self.log_view.setPlainText("API Key不能为空")
        return
        
    try:
        # 读取现有.env内容
        env_lines = []
        if os.path.exists('.env'):
            with open('.env', 'r', encoding='utf-8') as f:
                env_lines = f.readlines()
        
        # 更新或添加SILICONFLOW_API_KEY
        found = False
        with open('.env', 'w', encoding='utf-8') as f:
            for line in env_lines:
                if line.startswith('SILICONFLOW_API_KEY='):
                    f.write(f'SILICONFLOW_API_KEY={api_key}\n')
                    found = True
                else:
                    f.write(line)
            if not found:
                f.write(f'\nSILICONFLOW_API_KEY={api_key}\n')
                
        self.log_view.setPlainText("设置保存成功")
        self.reload_env()  # 重新加载环境变量
    except Exception as e:
        self.log_view.setPlainText(f"保存失败:{str(e)}")
        
        同时启动函数改为python_path启动不会导致电脑python版本不一致引起的 Traceback (most recent call last): File "D:\Whisper-Input\main.py", line 8, in <module> from src.audio.recorder import AudioRecorder File "D:\Whisper-Input\src\audio\__init__.py", line 10, in <module> from .recorder import AudioRecorder File "D:\Whisper-Input\src\audio\recorder.py", line 2, in <module> import sounddevice as sd ModuleNotFoundError: No module named 'sounddevice'失败问题。
        
         import sys
         def start_main(self):
    """启动main.py"""
    if not self.check_env_file():
        return
        
    if not self.get_api_key():
        self.log_view.setPlainText("请先输入SILICONFLOW API Key")
        return
        
    if self.process is None:
        logger.info("启动主程序")
        # 使用sys.executable获取当前Python解释器的路径
        python_path = sys.executable
        self.process = subprocess.Popen([python_path, "main.py"])
        self.start_btn.setEnabled(False)
        self.stop_btn.setEnabled(True)
        
        # 初始化日志显示
        self.update_log_view()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants