nikitka5703
nikitka5703

Reputation: 11

Running program on computers without python

I have written game with base on c++ and which uses python scripts to calculate object properties and draw them. But main problem is that it won't run on pc's without python2.7 installed on them. I'm out of ideas. How i should do that, to make it run on pc's without python on them?

Upvotes: 0

Views: 265

Answers (3)

Shmuel H.
Shmuel H.

Reputation: 2546

Python has a great API for C. You can use it in order to run your script. For example, this code will run a function from a python module:

#include <Python.h>

int main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc, *pValue;

    if (argc < 3) 
    {
        printf("Usage: exe_name python_source function_name\n");
        return 1;
    }

    // Initialize the Python Interpreter
    Py_Initialize();

    // Build the name object
    pName = PyString_FromString(argv[1]);

    // Load the module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference 
    pDict = PyModule_GetDict(pModule);

    // pFunc is also a borrowed reference 
    pFunc = PyDict_GetItemString(pDict, argv[2]);

    if (PyCallable_Check(pFunc)) 
    {
        PyObject_CallObject(pFunc, NULL);
    } else 
    {
        PyErr_Print();
    }

    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();

    return 0;
}

For more detailed info, see Extending Python with C or C++. See more example in the Demo/embed directory in the cpython source code.

Make sure you compile your code statically with python libraries. Otherwise, it will still require an installed version of python.

However, remember that you only have a python interpreter, not a full python installation. Therefore you would not be able to use almost any python modules without having them locally.

Upvotes: 3

user4891016
user4891016

Reputation:

Follow this tutorial: http://www.py2exe.org/index.cgi/Tutorial

You may have to make a few tweaks but you can convert your python script to and exe and dlls using py2exe.

Perhaps you could try converting your python to C using: http://cython.org/ Then referencing it with extern "C" in your C++ program:

extern "C"{
     //C Code Here
};

Could potentially reference the exported functions in the dlls output by py2exe.

Hopefully that may help.

Good Luck!

Upvotes: 0

Kolyunya
Kolyunya

Reputation: 6240

Make a game installer which will install all required dependencies.

Upvotes: 3

Related Questions