python - 致命的 Python 错误:无法获取随机数来初始化 Python
问题描述
致命的 Python 错误:无法获取随机数来初始化 Python
环境窗口 10、VSC 15
使用 CreateProcessA winapi 并传递 lpenvironment 变量以使用脚本运行 python。当 lpenvironment 为 null 时,它工作正常。如果我设置环境变量 PATH 和 PYTHONPATH = "paths",并传递该 LPSTR(env.c_Str()),它会在运行时抛出上述错误。python版本是3.5.6
有什么帮助吗?
更多细节。
- 我使用 CreateProcessA WINAPI 运行子进程 python.exe "C:\Program Files\endpoint\Python_ML\mlprocessor_server.py"。
- 我想用两个环境变量“PYTHONPATH”和“PATH”运行子进程。PYTHONPATH="C:\Program Files\endpoint\Python";"C:\Program Files\endpoint\Python\Scripts";"C:\Program Files\endpoint\Python\include";"C:\Program Files\endpoint \Python\Lib";"C:\Program Files\endpoint\Python\libs";"C:\Program Files\endpoint\Python\Lib\site-packages";"C:\Program Files\endpoint\Python_ML" 路径="C:\Program Files\endpoint\Python";"C:\Program Files\endpoint\Python\Lib";"C:\Program Files\endpoint\Python\Scripts";"C:\Program Files\endpoint\ Python\库"
由于某种原因,CreateProcessA 中的第 7 个参数失败,如果它为 null,则 python.exe 运行成功,否则会打印“致命的 Python 错误:无法获取随机数来初始化 Python”。
我设置参数的方式如下...
std::string Base = 配置::getBasePath();
std::string environPython = Base;
environPython.append("\\Python;");
environPython.append(Base);
environPython.append("\\Python\\Scripts;");
environPython.append(Base);
environPython.append("\\Python\\include;");
environPython.append(Base);
environPython.append("\\Python\\Lib;");
environPython.append(Base);
environPython.append("\\Python\\libs;");
environPython.append(Base);
environPython.append("\\Python\\Lib\\site-packages;");
environPython.append(Base);
environPython.append("\\Python\\_ML;");
environPython.push_back('\0');
std::string environPath = Base;
environPath.append("\\Python;");
environPath.append(Base);
environPath.append("\\Python\\Lib;");
environPath.append(Base);
environPath.append("\\Python\\Scripts;");
environPath.append(Base);
environPath.append("\\Python\\libs;");
environPath.push_back('\0');
std::string cmd = Base;
cmd.append("\\Python\\python.exe");
std::string params = "\"";
params.append(cmd);
params.append("\" \"");
params.append(Base);
params.append("\\Python\\_ML\\mlprocessor_server.py\"");
std::map env = { { "PYTHONPATH", environPython.data() }, { "PATH", environPath.data() }};
// example for generating block of strings
std::vector<char> envBlock;
std::for_each(env.begin(), env.end(),
[&envBlock](const std::pair<std::string, std::string> & p) {
std::copy(p.first.begin(), p.first.end(), std::back_inserter(envBlock));
envBlock.push_back('=');
std::copy(p.second.begin(), p.second.end(), std::back_inserter(envBlock));
envBlock.push_back('\0');
}
);
envBlock.push_back('\0');
// feed this into ::CreateProcess()
LPVOID lpEnvironment = (LPVOID)envBlock.data();
bool result = CreateProcessA(cmd.c_str(), (LPSTR)params.c_str(), NULL, NULL, FALSE, CREATE_NO_WINDOW, lpEnvironment, NULL, &info, &pi);
结果总是正确的,python.exe 没有显示在任务管理器中,并给出了致命的 Python 错误:未能获取随机数来初始化 Python。
如果 lpEnvironment 为 NULL,python.exe 将显示在任务管理器中。
解决方案
传递给的环境CreateProcessA
必须 include SYSTEMROOT
,否则在初始化期间在 python 内部调用时 Win32 API 调用CryptAcquireContext
将失败。
当传入 NULL 作为 lpEnvironment 时,您的新进程将继承调用进程的环境,该环境SYSTEMROOT
已经定义。
推荐阅读
- wpf - 故事板 重视静态资源
- go - 切片中的接口转换
- javascript - Electron 构建时显示白屏
- c++ - 是否可以使用“CMFCShellListCtrl”显示图像文件缩略图?
- java - 在单义性 csv 解析器中转义分隔符
- javascript - 选择符号后启用日期选择器
- php - Laravel 5.6 未在“已创建”模型事件中保存字段
- java - 如何使用 glide 库在带有 SQLite 的 gridview 中显示图像?
- javascript - 在自定义节点中从另一个函数调用一个函数 - NodeRED
- c - C交叉编译:sprintf仅在大小为[512][4]数[111][1]的数组初始化为0后写入“\0”字符