[英]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. 我已经基于c ++编写了游戏,并且使用python脚本来计算对象属性并绘制它们。 But main problem is that it won't run on pc's without python2.7 installed on them.
但是主要的问题是,如果没有在其上安装python2.7,它将无法在PC上运行。 I'm out of ideas.
我没主意了。 How i should do that, to make it run on pc's without python on them?
我应该怎么做,使其在没有python的PC上运行?
制作一个将安装所有必需依赖项的游戏安装程序。
Python has a great API for C. You can use it in order to run your script. Python有一个很棒的C语言API。您可以使用它来运行脚本。 For example, this code will run a function from a python module:
例如,以下代码将从python模块运行一个函数:
#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++ . 有关更多详细信息,请参阅使用C或C ++扩展Python 。 See more example in the
Demo/embed
directory in the cpython source code. 在cpython源代码的
Demo/embed
目录中查看更多示例。
Make sure you compile your code statically with python libraries. 确保使用python库静态编译代码。 Otherwise, it will still require an installed version of python.
否则,它将仍然需要安装的python版本。
However, remember that you only have a python interpreter , not a full python installation. 但是,请记住,您只有python解释器 ,而没有完整的python安装。 Therefore you would not be able to use almost any python modules without having them locally.
因此,如果没有本地的Python模块,您将几乎无法使用它们。
Follow this tutorial: http://www.py2exe.org/index.cgi/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. 您可能需要做一些调整,但是可以使用py2exe将python脚本转换为exe和dll。
Perhaps you could try converting your python to C using: http://cython.org/ Then referencing it with extern "C" in your C++ program: 也许您可以尝试使用以下命令将python转换为C: http : //cython.org/,然后在C ++程序中使用extern“ C”引用它:
extern "C"{
//C Code Here
};
Could potentially reference the exported functions in the dlls output by py2exe. 可能会在py2exe输出的dll中引用导出的函数。
Hopefully that may help. 希望这会有所帮助。
Good Luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.