Reputation: 1913
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.
Upvotes: 2
Views: 265
Reputation: 3954
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.
Upvotes: 1
Reputation: 335
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)
Upvotes: 2
Reputation: 148965
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.
Upvotes: 3