Files
952-11/HanddleAI/HanddleAi/python/pythonCpp.cpp
T
2024-09-12 17:01:38 +08:00

28 lines
999 B
C++

#include <windows.h>
#include <pythonCpp.h>
void pythonCpp::activateVirtualEnv(const std::string& pyPath, const std::string& venvName)
{
std::string venvPath = pyPath + "/" + venvName;
SetEnvironmentVariable("PYTHONHOME", pyPath.c_str());
SetEnvironmentVariable("PYTHONPATH", (venvPath + "/Lib;" + venvPath + "/Lib/site-packages").c_str());
}
bool pythonCpp::runPythonScript_R(const std::string& scriptPath) {
FILE* file = fopen(scriptPath.c_str(), "r");
if (file == nullptr) {
std::cerr << "Failed to open Python script: " << scriptPath << std::endl;
return false;
}
PyRun_SimpleFile(file, scriptPath.c_str());
fclose(file);
return true;
}
bool pythonCpp::runPythonScript(const std::string& scriptPath) {
PyObject* obj = Py_BuildValue("s", scriptPath.c_str());
FILE* file = _Py_fopen_obj(obj, "r+");
if (file == NULL) {
return false;
}
PyRun_SimpleFile(file, scriptPath.c_str());
Py_DECREF(obj);
return true;
}