Why Can't A C++ File Recognize A Python File?

I have a C++ file called Project.cpp and a Python file called ProjectFunctions.py. I decided to experiment and I #included the Python file:

It looks like on the first line of the C++ file where I #included the Python file, it doesn’t recognize def which is Python, which the C++ file doesn’t recognize. I understand that the C++ doesn’t recognize stuff included for a Python file. How can I use functions from a Python file by including in a C++ file?

It’s a bit of a pain to convert Python objects; running scripts and executing simple strings with an embedded intrepreter isn’t as bad but would be far less flexible and dynamic. Basic details at 1. Embedding Python in Another Application — Python 3.10.0 documentation with an → example for a function ← but you might want to search for other tutorials/guidance for this if you’re unfamiliar with it.


I don’t understand you.

He’s basically saying that it’s not as simple as just #include "filename.py".
There is several articles on the web about executing python from c++ and the reverse.
I suggest having a look there (and also at the links which @tgrtim so nicely provided you with) :slight_smile:

1 Like