2

I'm creating an application using Python 3.4.3 and tkinter for a project as this is the language I am strongest and most fluent in. However, for the specification it would make much more sense for the end user to have a compiled application to run. Is there any way to either compile Python (pretty sure there isn't) or a way to get C++, or any other compiled language, to run a Python script? Quite a broad question (sorry) and any hints and tips would be useful.

Rob Murray
  • 1,773
  • 6
  • 20
  • 32
  • 1
    Yes it's possible for a C++ program to call a python script. Easiest way would be to use `popen()`. See: http://stackoverflow.com/questions/16962430/calling-python-script-from-c-and-using-its-output. – Martin Nov 19 '15 at 15:24
  • 1
    If the only goal is to make the python code into an executable you could use something like [pyinstaller](http://www.pyinstaller.org/). – shuttle87 Nov 19 '15 at 15:25
  • You can also embed Python interpreter if needed, look at the docs: https://docs.python.org/2/extending/embedding.html – Ashalynd Nov 19 '15 at 15:28
  • Also one other thing you might consider is turning the problem on it's head: you can make your c++ code exposed as a python API using something like [boost python](http://www.boost.org/doc/libs/release/libs/python/). – shuttle87 Nov 19 '15 at 15:57

3 Answers3

3

My understanding is that what you want is to end with a single (Windows ?) executable that you could send to your end users to ease deployment.

It is possible (and easy) for scripts that only use a limited and know set of modules, there is even a nice tool on SourceForge dedicated to it: py2exe (following is extracted from tutorial).

You just prepare a setup.py script (when original script is trivially simple with a command line UI):

from distutils.core import setup
import py2exe

setup(console=['myscript.py'])

and run python setup.py py2exe

As soon as you are using a GUI and many modules, things can become harder, even if doc lets think that tkinter seems correctly processed.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • I'm sorry that this is just hints, but py2exe generally needs configuration and that configuration depends on what modules the python script uses. – Serge Ballesta Nov 19 '15 at 15:50
  • Indeed `pyinstaller` or `py2exe` are very good ways to start; There's no C++ code to write, not sure if that's what OP is asking, but runtime can be accessed just as well. – Dima Tisnek Nov 19 '15 at 16:12
2

What I've done in the past is to use the Python C API to load a Python module, call some methods, and convert values back into C types. See the documentation here: https://docs.python.org/3.4//c-api/index.html

I can't give detailed information here (read the docs), but here is a really basic example (I'm doing this off the top of my head, so there may be problems in my example, and it omits all error checking)

// first you would have to load the module
Py_Initialize();
PyObject *module = PyImport_ImportModule(module_name);

// you would want to do some error checking to make sure the module was actually loaded

// load the module dictionary
PyObject *module_dict = PyModule_GetDict(module);

// call the constructor to create an instance
PyObject *constructor = PyDict_GetItemString(module_dict, "ClassName");
PyObject *instance = PyObject_CallObject(constructor, NULL);
Py_DECREF(constructor);

// call a method that takes two integer arguments
PyObject *result = PyObject_CallMethod(instance, "method_name", "ii", 5, 10);

// let's pretend the result is an integer
long log_val = PyInt_AsLong(result)
Glen
  • 315
  • 2
  • 10
1

I have used Nuitka and it really works like a charm. Nuitka translates python code to C++ and compile it, creating an executable. You can tell Nuitka what part of python code must be compiled: only your code, your code and some libraries, or everything, including linking with the python interpreter to get a stand alone executable.

Cython works great too (although I experimented some problema when using de super function in derived clases). In this case you need the interpreter.

eguaio
  • 3,754
  • 1
  • 24
  • 38