python打包exe不工作_python在调试模式下可以在c ++中工作,但不能在exe文件中工作...

I am working on a Python code embedding in c++ helloworld program,

necessary additional include/library directories are properly set up.

When I use Local Windows Debugger, it shows "Hello World" correctly.

But if I double click project.exe, it says project.exe has stopped working.

Does anyone know what kind of configurations or steps to make so that project.exe shows "Hello World" when double clicked??

Code goes like the following:

main.cpp

#include

#include

#include

#include

using namespace std;

int main()

{

Py_Initialize();

PyRun_SimpleString("import sys");

PyRun_SimpleString("sys.path.append('./')");

PyObject *pModule = PyImport_ImportModule("helloworld");

PyObject *pFunc = PyObject_GetAttrString(pModule, "printHello");

PyEval_CallObject(pFunc, NULL);

Py_Finalize();

return 0;

}

helloworld.py

def printHello():

print("Hello World!")

解决方案

Shot in the dark:

You're not checking the return values for your calls, specially the ones returning pointers

running with the debugger isn't using the same directory as the "exe-clicking" method

you should check the return value of pModule. My guess is that since you're in a different directory, the import fails, so the PyImport_ImportModule function returns NULL (python raises exceptions, but not in this context as this is a C API with its limitations)

This is fragile (and possibly useless):

sys.path.append('./')

You don't know what the current directory is. It would be better to make it relative to the current executable, or configurable with an argument or an environment variable. You could make it relative to the current executable, see Finding current executable's path without /proc/self/exe or Get path of executable

Now, when you try to use this null pointer, the program crashes. Start with this:

PyObject *pModule = PyImport_ImportModule("helloworld");

if (pModule == NULL)

{

std::cout << "could not import module\n";

exit(1);

}

(same goes for the attribute fetch: always protect your calls, or better: wrap them in a C++ method that throws exceptions).

#include

#include

PyObject *safe_PyImport_ImportModule(const std::string &module_name)

{

PyObject *pModule = PyImport_ImportModule(module_name.c_str());

if (pModule == NULL) // c++11 purists would put "nullptr"

{

std::cout << "cannot import " << module_name << '\n';

throw std::runtime_error("Import error: "+module_name);

}

return pModule;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值