0

I wanted to try out embedding Python into C++. I was able to get that to work but I wanted to start writing prints with variables which are in declared in c++. For example:

(C++)

int num = 43;
PyRun_SimpleString("print("+num+")");
char g;
std::cin>>g;
PyRun_SimpleString("print("+g+")");

I tried to figure out how to use other related functions, but I don't seen to find enough information.

1 Answers1

2

To pass char,

Python script:

def test(person):
    return "Hello " + person;

C++:

PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;
pName = PyUnicode_FromString((char*)"script");
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, (char*)"test");
pArgs = PyTuple_Pack(1, PyUnicode_FromString((char*)"User"));
pValue = PyObject_CallObject(pFunc, pArgs);
auto result = _PyUnicode_AsString(pValue);
std::cout << result << std::endl;

Output:

Hello User

To pass integer it's the same like above. Here you are passing over double 2.0.

pArgs = PyTuple_Pack(1,PyFloat_FromDouble(2.0));
pValue = PyObject_CallObject(pFunc, pArgs);

You can refer to all the apis here >> https://docs.python.org/3/c-api/

Sin Han Jinn
  • 574
  • 3
  • 18
  • If you don't mind me asking but how would you use this? I'm not at all familiar with this. – user20342224 Nov 24 '22 at 02:29
  • I updated my code from initialization up till receiving results so it's easier. since you can run pyrunsimplestring, means your embedding has no issues. "script" is the name of your python file, script.py. while "test" is your function name. you can try it to understand – Sin Han Jinn Nov 24 '22 at 02:35
  • Thanks, but I'm running into an access violation reading location. Its being caused on the pFunc line. I set the py file; do I have to do anything with the module? – user20342224 Nov 24 '22 at 02:50
  • The pFunc line is loading the script. means possibly it's not able to find your script. make sure the naming is correct and the script location is within your environment. you can test it by adding the script on the same directory of your c++ exe OR you can set environment variable PATH for your user/admin to wherever your python file directory is. – Sin Han Jinn Nov 24 '22 at 02:54
  • Yes, I have my py file inside of the same folder location as my cpp file. – user20342224 Nov 24 '22 at 02:58
  • Hmm I'm not sure how you are running your cpp files. It's better you set environment path to your python file . You can refer to instructions here 0> https://docs.oracle.com/en/database/oracle/machine-learning/oml4r/1.5.1/oread/creating-and-modifying-environment-variables-on-windows.html – Sin Han Jinn Nov 24 '22 at 03:10
  • wdym where my exe is? Like the release file @sin han jinn – user20342224 Nov 24 '22 at 03:13
  • @user20342224 yeah there are many ways to run cpp. yeah i meant your release file. have you tried the environment path? – Sin Han Jinn Nov 24 '22 at 03:38
  • So, I moved the py to the release folder which resolved the first issue; but now there is another error at PyObject_CallObject(). Now I am getting a nullptr from pFunc. – user20342224 Nov 24 '22 at 03:47
  • I used PyErr_Print(); and got a "AttributeError: module 'script' has no attribute 'test'". This doesn't seem right. Is test supposed be a module?? – user20342224 Nov 24 '22 at 04:07
  • I think the problem I have is that my py file is encoded in western european. – user20342224 Nov 24 '22 at 04:13
  • the "test" suppose to be the function defined in your python script. maybe the structure is different in your script. you can see the example structure above. you may need to research a little if you want to conform to a different structure. – Sin Han Jinn Nov 24 '22 at 04:23
  • OMG I AM SOO DUMB!!! I had a duplicate file with the same name and I was typing into instead of the actual file in the release! THANK YOU SO MUCH for helping me for this long :) – user20342224 Nov 24 '22 at 04:32
  • Nup you're not dumb, it happens to everyone :) The best advice is always take a breather when you can't fix something, when you get back, you'll almost certainly solve them. Most welcome though! – Sin Han Jinn Nov 24 '22 at 05:42