0

I have this C code i took from https://docs.python.org/3/extending/embedding.html:

#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("import numpy as np \n"
"student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])\n" 
"print(student)\n");
    if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}

It compiles and runs quite nicely in a machine that has python3 and python3-numpy installed. The output:

[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]

However, if the target machine does not have numpy installed, the output is:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

My question: is it possible to run the python code from the app executable in a target machine that has no Python3 nor numpy installed? If so, how do i do it?

I mostly thinking of Win10 as a target machine, but i guess my question would equally apply to other OSes.

Thanks!

EDIT: To be more exact, I am not asking if this code can be compiled without having Python installed. I know it must have some python-dev to be compiled.

But what I want to know is there a way to create an .exe file that uses some Python calls without having Python installed in the target machine?

  • Where do you expect your program to get all its Python functionality from without Python? – Emanuel P Apr 16 '21 at 19:55
  • This code clearly calls Python and the Python numpy library, so no, it cannot be done. But I imagine it should be possible to install them on a Win10 box too. – joanis Apr 16 '21 at 20:13

1 Answers1

0

Use PyInstaller. It will wrap your Python code, a Python interpreter, and the packages you need into a standalone .exe file.

Specifically, numpy is on the list of supported packages.

I would get rid of the C program, put your main Python code in a .py file, and let PyInstaller do the work for you.

This is a much better solution than requiring your users to have a Python interpreter installed, much less any particular libraries and packages. It allows each application to have its own version of Python and packages.

Full details are in the PyInstaller docs.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • Thanks! However, i cannot get rid of the C program, since the main work is done by the C part. The python will only create a few animation files in new launched processes. – user13899543 Apr 16 '21 at 21:28
  • Ah yes, that changes things. Here's another idea: Do your C and Python code need to share data in memory? Or would it work to run the Python code in a separate process launched from the C code, and share data using files or stdin/stdout or named pipes or...? If a separate Python process works, then you could use PyInstaller to build that executable. – Michael Geary Apr 16 '21 at 21:47
  • One more idea while we're brainstorming: You could invert this and make Python be your main program, compile your C code into a DLL, and use either the native Python/C API or something like Clif or Swig to make it easier to call if you use C++. Either way you could still use PyInstaller to bundle your binaries together. – Michael Geary Apr 17 '21 at 10:13