0
#define PY_SSIZE_T_CLEAN
#include <Python.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
                       "print('Today is', ctime(time()))\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

embedding python in C

I tried the example given by the official tutorial. it worked on condition that the python path is set in environment path variables. What if the python environment is embeded in the C project, how can I assign the python path dynamically within the code. I've tried _putenv method before the Python Virtual Machine is initiated, but in vain. As is instructed in the Python/C Api Doc, some function can be called before the initialization like Py_SetPath or Py_SetPythonHomein doc, it cast an error like "pythonxx.dll" cannot be found. I don't know how to do then?

gcc 9.2.0 mingw 32bit
windows10
python3.8.10 32bit
Arthur
  • 63
  • 1
  • 8
  • What are the expected and actual results? What's the *OS* and build commands? [\[SO\]: How to create a Minimal, Reproducible Example (reprex (mcve))](https://stackoverflow.com/help/minimal-reproducible-example). Comment `Py_SetProgramName(program);`. – CristiFati Mar 24 '23 at 16:42
  • @CristiFati on Windows,```gcc -o test.exe test.c -Wall -lpython38 -L pythonpath\libs -I pythonpath\include```. Actually the output ```test.exe``` would work on condition that I've set system environment path for the embeded python directory or just put the executable right in the python directory . What I want to do is that whether I can set the python environment path in the ```test.c```. And I've tried to set it with Windows api ```_putenv```, but it seems to be in vain. I don't know how. – Arthur Mar 25 '23 at 05:39
  • @CristiFati is your python env is Conda env which has already been add to the system path list? do you encounter "pythonxx.dll" not found error – Arthur Mar 25 '23 at 15:19
  • It works, both in *VEnv* and outside it (didn't try it in *Conda*). I also added `printf("Exe: %S\nPath: %S\nExecPrefix: %S\n", Py_GetProgramFullPath(), Py_GetPath(), Py_GetExecPrefix());` and it prints values. – CristiFati Mar 25 '23 at 15:55
  • @CristiFati well, it can be kind of hard to tell the difference between your dealing with this project with mine in detail – Arthur Mar 26 '23 at 06:59
  • The question still lacks details like the toolchain used, and the *Python* version attempted (native or part of that toolchain). But it's most likely a duplicate of [\[SO\]: Why does adding multiprocessing prevent python from finding my compiled c program? (@CristiFati's answer)](https://stackoverflow.com/a/75035737/4788546) (or [\[SO\]: Can't import dll module in Python (@CristiFati's answer)](https://stackoverflow.com/a/59333711/4788546) ). Following the guidelines there you should get to the bottom of it. – CristiFati Mar 26 '23 at 09:51
  • @CristiFati thanks. it's just the python 3.8.10 / 32bit. python38.dll cannot be found. – Arthur Mar 27 '23 at 13:10

0 Answers0