简体   繁体   English

在C ++中嵌入python时模块出现问题

[英]Trouble with modules while embedding python in C++

I'm making a .dll for an .exe program and embedding python in it. 我正在为.exe程序制作.dll,并将python嵌入其中。 It worked fine with this simple .py program 使用这个简单的.py程序可以正常工作

from time import *
##import OptimRestriction

def test_callsign(b):
    ...(simple script)
    return 

What I did was copy the .py program, the Dll and Lib folders into the xxx.exe folder, like is said here . 我所做的就是将.py程序,Dll和Lib文件夹复制到xxx.exe文件夹中,就像这里所说的。

But as soon as I uncomment the import of OptimRestriction the debug crashes. 但是,一旦取消注释OptimRestriction的导入,调试就会崩溃。 First it starts loading symbols when the call to the thread that initializes and deals with Python is called: _ctypes.pyd , _sockets.pyd , sst_pyd , harshlib.pyd , unicodedata.pyd , tkinter.pyd , all modules that the OptimRestriction does not use. 首先,当调用初始化和处理Python的线程的调用时,它开始加载符号: _ctypes.pyd_sockets.pydsst_pydharshlib.pydunicodedata.pydtkinter.pydOptimRestriction不使用的所有模块。

The error given after the debug crashes is: 调试崩溃后给出的错误是:

Unhandled exception at 0x1E0AA0C5 (python27.dll) in xxx.exe: 0xC0000005: Access violation reading location 0x00000004.

And it breaks on the _callthreadstart function. 并且它在_callthreadstart函数上_callthreadstart

OptimRestriction is a long program that imports a lot of modules (that are also in the .exe folder). OptimRestriction是一个很长的程序,它会导入很多模块(也位于.exe文件夹中)。 Here's the list of its imports: 以下是其进口清单:

from GrafFunc import *
from LogFunc import *
from DinamicaFunc import *
from Dinamica2 import *
from CDR import *
...
import sys
import cProfile"

What it seems to me is that the thread takes too long to start because the debug starts loading those files for a long time, and so it gives an error. 在我看来,该线程启动所需的时间太长,因为调试开始长时间加载那些文件了,因此会产生错误。 Am I correct? 我对么? And if so, why is it loading those files if OptimRestriction and its impots don't use them? 如果是的话,如果OptimRestriction及其提示不使用它们,为什么OptimRestriction加载这些文件?

Edit:New information. 编辑:新信息。 It crashes saying Can't import module on this line: pModule = PyImport_Import(pName); 崩溃提示Can't import module在此行上Can't import modulepModule = PyImport_Import(pName);

After long hours of reducing the problem to some lines of code, i just found out that the problem was in the lines where some of the modules opened .txt files to read. 经过长时间的研究,将问题简化为一些代码行,我才发现问题出在某些模块打开.txt文件进行读取的行中。

I thought that having the .txt files in the same folder of the .py programs was the correct thing to do, but it seems that I needed to copy them to the folder of my c++ plugin (I think that's because while I'm debugging, the system path is changed for my plug-in folder since the writing and reading of pyhton is done to/from there) 我认为将.txt文件放在.py程序的同一文件夹中是正确的做法,但是似乎我需要将它们复制到我的c ++插件的文件夹中(我认为这是因为在调试时,因为pyhton的读写是在此完成,所以我的插件文件夹的系统路径已更改)

Problem solved! 问题解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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