简体   繁体   English

在没有python的计算机上运行程序

[英]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.

相关问题 Python没有运行我的程序而没有任何错误 - Python not running my program without any error 使用/不使用 sudo 运行 python 程序的一部分 - Running part of python program with/without sudo 运行Python程序命令而不安装它 - Running a Python program command without installing it 在没有“./”的命令行中运行 python 程序 - running a python program in the command line without "./" 在python程序中查找变量列表而无需显式运行程序 - Finding a list of variables in python program without running the program explicitly 从其他计算机运行python脚本 - Running a python script from different computers 如何使我的 Python 程序提供的功能可用于在同一台计算机或其他计算机上运行的其他语言编写的程序? - How can I make the functions provided by my Python program available to programs written in other languages running on the same or other computers? 在调试中运行程序而无需进行硬编码DEBUG =在Python中为True - Running a program in debug without hard coding DEBUG = True in Python 在没有线程的情况下运行程序时更改变量[Python] - Changing variables while a program is running without threading [Python] 从python运行外部程序:无需等待所有输出的管道 - Running an external program from python: piping without waiting for all output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM