Calling a python file from C++ using Xcode in Mac 10.15

I wanted to run a .py (python) file from XCODE (C++). Below is the code I have written.

int main(int argc, const char * argv[]) 
{
char filename[1024];
strcpy(filename, "/<some location>/downloader.py");
FILE* fp; Py_Initialize();
fp = fopen(filename, "r");
PyRun_SimpleFile(fp, filename);
Py_Finalize(); return 0;
}

In this "downloader.py", I have imported module 'requests', 'sys' etc to achieve certain functionality. This requires python3. This is working fine when I ran from PyChamCE editor (which is having python 3.7 as its configuration).

When I am calling the same "downloader.py" from my C++ program, it hangs in "PyRun_SimpleFile(fp, filename);". And, this is happening when I have 'requests' module imported in my "downloader.py" (import requests). And this is not happening when I remove the line "import requests" from "downloader.py".

The default python shown (I ran python --version in a terminal) is "Python 3.8.2". I have changed the default python 2.7.16 to 3.8.2 by running "python='/usr/local/bin/python3'". And, python3 is having 'requests' module in it.

I have below questions (doubts).

  1. Why "PyRun_SimpleFile(fp, filename);" line is hanging though I set the default python as 3.8.2.
  2. Do I need to do any specific settings in XCODE (I am using XCODE 11 in macOS 10.15.5).

Any help would be really appreciable.


Regards,

Sankaran

Calling a python file from C&#43;&#43; using Xcode in Mac 10.15
 
 
Q