0

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?

2
  • 2
    You want to run Python without Python? Commented Nov 20, 2016 at 11:58
  • One possible solution is to have Cython compile the Python functions to DLLs/shared-libraries, and include these in your code and make. Commented Nov 21, 2016 at 19:05

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

With python installed code runs properly, but i need to make it so it will run on pc without python installed on it.
@SergeBallesta, Yes, see here for more information: github.com/python/cpython/tree/2.7/Demo/embed. Make sure you compile it statically.
The statical link is what is was missing from your answer. But anyway, it will only work with core or native functions. But at soon as you import a pure Python module from the standard library, you loose: if you have no Python installation, where will you find the Python (source) module?
@SergeBallesta I have updated the answer. You are right about the standard library, however, you still can copy the required modules with your program. I would not recommend that for python scripts with a lot of dependencies. In this case, it maybe easier to port the module to C instead.
Add the limitation for the modules from the standard library and I'll upvote.
3

Make a game installer which will install all required dependencies.

Comments

0

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!

2 Comments

He want to embed a python program into a C++ program. Not to run python without a local python interpreter.
In that case you could use the exported functions in the dlls and call them in c++ perhaps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.