首页 > 解决方案 > 致命的 Python 错误:无法获取随机数来初始化 Python

问题描述

致命的 Python 错误:无法获取随机数来初始化 Python

环境窗口 10、VSC 15

使用 CreateProcessA winapi 并传递 lpenvironment 变量以使用脚本运行 python。当 lpenvironment 为 null 时,它工作正常。如果我设置环境变量 PATH 和 PYTHONPATH = "paths",并传递该 LPSTR(env.c_Str()),它会在运行时抛出上述错误。python版本是3.5.6

有什么帮助吗?


更多细节。

  1. 我使用 CreateProcessA WINAPI 运行子进程 python.exe "C:\Program Files\endpoint\Python_ML\mlprocessor_server.py"。
  2. 我想用两个环境变量“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 将显示在任务管理器中。

标签: pythonc++windowsvisual-studio-2015windows-10

解决方案


传递给的环境CreateProcessA必须 include SYSTEMROOT,否则在初始化期间在 python 内部调用时 Win32 API 调用CryptAcquireContext将失败。

当传入 NULL 作为 lpEnvironment 时,您的新进程将继承调用进程的环境,该环境SYSTEMROOT已经定义。


推荐阅读