Last active
August 30, 2023 21:30
-
-
Save Infinitusvoid/f252d5f8334ee0f384a5e548c5ecdad9 to your computer and use it in GitHub Desktop.
C++20 : How to execute python from Cpp ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("This is python") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment