Skip to content

好像错误地附加了一个额外的 .json 后缀 #30

@san-ren

Description

@san-ren
  • Python 3.11.9
  • Name: opencc-python-reimplemented
    Version: 0.1.7
  • win11

报错:

初始化 OpenCC 转换器失败: [Errno 2] No such file or directory: 'D:\ruanjian\my-project\Qt_pdf_converter\.venv\Lib\site-packages\opencc\config\t2s.json.json'。

Image

  • 依赖默认的查找机制

opencc_converter_instance = None 
OPENCC_AVAILABLE = False
try:
    from opencc import OpenCC
    # 修正这里:确保是 't2s.json'
    opencc_converter_instance = OpenCC('t2s.json') 
    OPENCC_AVAILABLE = True
    print("成功导入 OpenCC 库,繁简转换功能已启用。")
except ImportError:
    print("未检测到 OpenCC 库。繁体转简体功能将禁用。")
except Exception as e:
    # 这里的 e 应该就是你看到的 FileNotFoundError
    print(f"初始化 OpenCC 转换器失败: {e}。繁体转简体功能将禁用。") 
    opencc_converter_instance = None # 初始化失败,确保实例为 None
    OPENCC_AVAILABLE = False # 标记为不可用
  • 给 OpenCC() 提供配置文件的绝对路径
opencc_converter_instance = None 
OPENCC_AVAILABLE = False
try:
    from opencc import OpenCC
    import platform # 需要导入 platform

    # --- 获取 opencc 库内 config 目录的绝对路径 ---
    opencc_pkg_path = ""
    try:
        # 尝试找到 opencc 包的安装路径
        import opencc
        opencc_pkg_path = os.path.dirname(opencc.__file__)
        print(f"DEBUG: Found opencc package path: {opencc_pkg_path}")
    except ImportError:
        print("DEBUG: Could not import opencc to find package path.")
    except AttributeError: # 如果 opencc 没有 __file__ 属性 (不太可能)
         print("DEBUG: opencc module does not have __file__ attribute.")

    config_dir_path = os.path.join(opencc_pkg_path, 'config') if opencc_pkg_path else ""
    t2s_config_path = os.path.join(config_dir_path, 't2s.json') if config_dir_path else ""
    
    print(f"DEBUG: Attempting to find t2s config at: {t2s_config_path}")

    if os.path.exists(t2s_config_path):
        print(f"DEBUG: Found absolute path for t2s.json: {t2s_config_path}")
        # --- 使用绝对路径初始化 OpenCC ---
        opencc_converter_instance = OpenCC(t2s_config_path) 
        OPENCC_AVAILABLE = True
        print("成功导入 OpenCC 库,并使用绝对路径配置,繁简转换功能已启用。")
    else:
        # 如果绝对路径也找不到文件,说明安装真的有问题,或者路径计算错误
        print(f"错误: 无法在计算出的绝对路径下找到 t2s.json: {t2s_config_path}")
        raise FileNotFoundError(f"t2s.json not found at calculated path: {t2s_config_path}")

except ImportError:
    print("未检测到 OpenCC 库。繁体转简体功能将禁用。")
except Exception as e:
    print(f"初始化 OpenCC 转换器失败: {e}。繁体转简体功能将禁用。") 
    opencc_converter_instance = None 
    OPENCC_AVAILABLE = False 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions