致命的Python错误:init_fs_encoding:无法获取文件系统编码的Python编解码器。在 Qt Creator 中使用 <Python.h> 时出现问题

问题描述 投票:0回答:1

我正在尝试在 Qt Creator 项目运行时(C++ 中)期间执行 python 文件。为了做到这一点,我下载了 python311 并将这些行添加到我的 .pro 文件中:

INCLUDEPATH += "C:\Users\(name)\AppData\Local\Programs\Python\Python311\include"

LIBS += -L"C:\Users\(name)\AppData\Local\Programs\Python\Python311\libs" -l"python311"

然后,在我的应用程序中(特别是在我的 mainwindow.cpp 文件中),我运行以下代码以执行 python 文件:

FILE* fp;
const char* file = "C:/(absolute path)/(filename).py";
Py_Initialize();
fp = fopen(file, "r");
PyRun_SimpleFile(fp, file);
Py_Finalize();

但是,在运行程序时,调用此程序时程序会崩溃。使用调试模式,应用程序输出部分包含以下错误消息:

mincore\com\oleaut32\dispatch\ups.cpp(2126)\OLEAUT32.dll!00007FFF014C470C: (caller: 00007FFF014C49FA) ReturnHr(1) tid(6750) 8002801D Library not registered.
Python path configuration:
  PYTHONHOME = 'C
:\Qt\Tools\m
ingw
1120
_64\bi
n\..
\opt'
  PYTHONPATH = (not set)
  program name = 'p
ytho
n'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = 'C
:\Qt\
Tools\mingw
1120_64
\opt\Li
b'
  sys._base_executable = 'C:\\Users\\(name)\\(one drive)\\Documents\\(project name)_Qt_6_5_1_MinGW_64_bit-Debug\\debug\\(project name).exe'
  sys.base_prefix = 
'C:\\Qt\\Tools\\mingw1120_64\\bin\\..\\opt'
  sys.base_exec_prefix = 'C:\\Qt\\Tools\\mingw1120_64\\bin\\..\\opt'
  sys.platlibdir = 'DLLs'
  sys.executable = 'C:\\Users\\(name)\\(one drive)\\Documents\\(project name)_Qt_6_5_1_MinGW_64_bit-Debug\\debug\\(project name).exe'
  sys.prefix = 'C:\\Qt\\Tools\\mingw1120_64\\bin\\..\\opt'
  sys.exec_prefix = 'C:\\Qt\\Tools\\mingw1120_64\\bin\\..\\opt'
  sys.path = [
    'C:\\Users\\(name)\\AppData\\Local\\Programs\\Python\\Python311\\python311.zip',
    'C:\\Qt\\Tools\\mingw1120_64\\opt\\DLLs',
    'C:\\Qt\\Tools\\mingw1120_64\\opt\\Lib',
    'C:\\Users\\(name)\\(one drive)\\Documents\\(project name)_Qt_6_5_1_MinGW_64_bit-Debug\\debug',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00006750 (most recent call first):
  <no Python frame>
Fatal Python error: 
failed to get the Python codec of the filesystem encoding

有人可以帮助我了解如何解决这个问题吗?我对 python 缺乏经验,因此不明白错误消息。我在引用的不同 python 版本中看到一个潜在问题(一个是我自己下载的:311,另一个是使用 Qt mingw64 安装程序自动安装的),但除此之外,我不明白出了什么问题。

我尝试从命令行和高级设置 GUI 调整 PYTHONPATH 和 PYTHONHOME 变量,但这些似乎对程序的结果没有影响(尽管我可能没有正确更改它们)。

c++ python-3.x qt fatal-error modulenotfounderror
1个回答
0
投票

为了扩展上面 Ryan 的答案,在没有环境变量 PYTHONHOME 的情况下,Python 使用可执行文件的父目录作为 Python 环境。

© www.soinside.com 2019 - 2024. All rights reserved.