Skip to content

Instantly share code, notes, and snippets.

@Infinitusvoid
Last active August 30, 2023 21:30
Show Gist options
  • Save Infinitusvoid/f252d5f8334ee0f384a5e548c5ecdad9 to your computer and use it in GitHub Desktop.
Save Infinitusvoid/f252d5f8334ee0f384a5e548c5ecdad9 to your computer and use it in GitHub Desktop.
C++20 : How to execute python from Cpp ?
#include <iostream>
#include <cstdlib>
int executePythonFromCpp(const std::string& file_path_to_python, const std::string& file_path_to_script)
{
std::string command = file_path_to_python + " " + file_path_to_script;
int result = system(command.c_str());
// Check the result of the system call
if (result == 0) {
std::cout << "Python script executed successfully." << std::endl;
}
else {
std::cout << "Error executing Python script." << std::endl;
}
return result;
}
int main()
{
std::cout << "Wind" << "\n";
std::string file_path_to_python = "C:/Users/Cosmos/AppData/Local/Programs/Python/Python311/python.exe";
std::string file_path_to_script = "script.py";
executePythonFromCpp(file_path_to_python, file_path_to_script);
return 0;
}
print("This is python")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment