I want to learn how to execute Python programs from within C code. How is this done, for example using the simplest of C and Python programs as shown below (the classic "Hello World" programs)? For now I want to learn how to do this on my Windows 10 desktop where I do have Visual Studio and can code and execute C/C /Python; a longer term goal will be to do this sort of thing on a Raspberry Pi. Any help greatly appreciated. I have Python 3 installed on my Windows 10 PC.
// C Program
#include <iostream>
int main() {
std::cout << "Hello World!";
// How can the above line of code be changed so that
// 'Hello World!' is output using a call to a simple
// hello.py program ?
return 0;
}
CodePudding user response:
In Visual Studio, you'll need to make sure the relevant Python include directories and libraries are linked against your C project. First, find where Python is installed on your system; for me it is in C:\Python39
but you may have to go on a bit of a wild goose chase if Windows has decided to bury it somewhere else.
Make sure that your target architecture matches the Python architecture; this almost certainly means making sure that you set the dropdown next to the big friendly Local Windows Debugger
button to x64
.
Next, in your Visual Studio project, lets say it's named PythonTestProject
, you'll need to right click on the project name in the Solution Explorer window and click properties. Then navigate to Configuration Properties -> C/C -> General
and add the directory C:\Python39\include
to the Addition include directories
field, of course changing the path prefix to wherever you installation is. Then navigate to Configuration Properties -> Linker -> Input
and prepend C:\Python39\libs\python3.lib;
(note the semicolon) to the Additional dependencies
field. Your project is now good to be run against Python.
To run a simple file, you need the PyRun_SimpleFile
(the simple here doesn't actually refer to how complicated the code in the file is, as much as the simple calling convention) which is provided in Python.h
. You'll also need to initialize the interpreter before calling this function, and optionally clean it up afterwards although this will be done automatically when your program ends. So something like this:
#include <Python.h>
#include <stdio.h>
int main()
{
// Initialize the Python interpreter
Py_Initialize();
// Open a script and run it
const char* filename = "hello.py";
FILE* f = fopen(filename, "rb");
PyRun_SimpleFile(f, filename);
// Clean up any memory and state allocated by the Python interpreter
fclose(f);
Py_Finalize();
}
should do the trick. Make sure that hello.py
is in the same directory as the rest of the source files in your project, hit that big friendly debug button and it should run you file.
Hope this gets you on the right track :). I do strongly recommend looking into a C library such as pybind11 which will make your life much (much much) easier than dealing directly with the C API. If you want to just do simple things like this though, then it is not worth the trouble; the functions described in the C API documentation can do a lot of simple tasks very easily.
CodePudding user response:
On C/C , you can use the system command to execute any shell command. That would allow you to use just about any other language, including Python, Node.js, C#... docs
#include <cstdlib>
int main(){
system("echo hello world");
}