简体   繁体   中英

How to execute a C++ code with a python code embedded on a system without python installed

I'm trying to create a simple game using C++ with Python embedding. The python code is embedded in my C++ code. I used Python/C API for this. There are 2 goals that I would like to achieve:

1) Application should be able to run on a computer that does not have Python installed.

2) Application should be an one standalone executable file(.exe)

For example, the following simple code only works if the system has python already installed on it:

#include <Python.h>
#include <conio.h>
#include <stdio.h>

int main()
{
char pySearchPath[] = "Python27"; 
Py_SetPythonHome(pySearchPath); 

Py_Initialize();
PyRun_SimpleString("print 'Hello World'");
PyRun_SimpleString("for i in range(5):\n"
    "\tprint i,\n");
Py_Finalize();
getch();
return 0;
}

Python needs to be installed on the computer or these files should be in the executable's directory:

        python27.dll
        Python27\
            DLLs\ (contents of DLLs folder)
            Lib\ (contents of Lib folder)

Question : How can I include/add/bind these files within my .exe so that my application can execute on a system which has no python installed? I would like to have a standalone executable. Is it possible? If yes then please provide some hints about how to accomplish this? I already invested much of time searching in internet but unfortunately I didn't find any thing useful.

Probably, there is an other possibility to solve this problem, however I am not sure. For example could the following method work?

Python-Code ----transforming----> bytecode -----> native code -------> import in programm

UPD : With Cython I generate .pyd file (.dll). How can I generate .h file from its or call some functions in my app(if it is possible, of course)?

Thank you.

Yes, you can embed python in C++. Google provided me two links that I think can be useful for you. I am not sure if you already knew these links as you didn't mention in your question about your research.

http://www.codeproject.com/Articles/11805/Embedding-Python-in-CC-Part-I This link has two part tutorial on this topic.

http://realmike.org/blog/2012/07/05/supercharging-c-code-with-embedded-python/ This link is also discussing the same topic.

It is basically avoided to paste off site links in the answer. But unfortunately the answer to your question is too long that I am forced to provide the links here. Sorry for that. Just to be safe you should download the whole tutorial from these links, so that you don't regret in future if the tutorials become offline!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM