【发布时间】:2020-03-15 04:08:21
【问题描述】:
当我尝试使用 c++ 调用 python 时:
QString command = "cd C:\\python\\python37 && python C:\\projects\\file_editor.py"
QByteArray ba = command.toLocal8Bit();
const char *c_str2 = ba.data();
std::system(c_str2)
我收到此错误:
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
任何想法如何解决这个问题?
编辑: 我在 PATH 变量中的 python 相关路径是:
C:\python\python37\Scripts
我可以使用 Qt 我也试过这个:
QProcess process;
QStringList pythonCommandArguments = QStringList() << "C:\\projects\\file_editor.py"
process.start("python", pythonCommandArguments);
然后我得到这个错误:
QProcess: Destroyed while process ("python") is still running.
【问题讨论】:
-
std::system(const('cd C:\python\python37 && python.exe C:\projects\file_editor.py'))不是有效的 C++ 代码。 -
@Lua,你确定吗?
\f是一个有效的转义字符,我想知道您为什么要痛苦地将/替换为 `\`。 -
这看起来像是 Python 错误,与 C++ 无关。
-
如果你这样做
std::system("cd C:\\python\\python37 && python C:\\projects\\file_editor" )你会得到错误吗? -
@Lua 我知道,但要调试问题,这是过程的一部分。将您的代码更改为
std::system("cd C:\\python\\python37 && python C:\\projects\\file_editor");并查看您是否收到错误。如果你不这样做,那么你就知道你的字符串格式不正确。