如何将自制的32位cpp DLL加载到我的机盖3.5 32位python中?

时间:2019-06-21 07:07:38

标签: python dll canopy

我正在尝试将包含CPP函数的DLL(32位)文件加载到python中。它适用于python 3.7(32位),但是在使用冠层3.5(32位)时会出现错误。

我用来加载dll的代码:

import os
import ctypes

os.chdir(r"G:\DLLdirectory")
mydll = ctypes.cdll.LoadLibrary('MyDLL.dll')

如果我在pyton 3.7上运行它,则可以正常工作;如果在冠层3.5上运行,则可以得到:

Traceback (most recent call last):
    File "DIR/PythonFile.py", line 26, in <module>
        mydll = ctypes.cdll.LoadLibrary('MyDLL.dll')
    File "DIR\Canopy32\edm\envs\User\lib\ctypes\__init__.py", line 425, in LoadLibrary
        return self._dlltype(name)
    File "DIR\Canopy32\edm\envs\User\lib\ctypes\__init__.py", line 347, in __init__
        self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] The specified module could not be found

如果在顶篷中将os.chdir()更改为sys.path.append(),仍然找不到该模块,并且在python 3.7中出现此错误:

Traceback (most recent call last):
    File "DIR/PythonFile.py", line 26, in <module>
        mydll = ctypes.cdll.LoadLibrary('MyDLL.dll')
    File "DIR\Python\Python37-32\lib\ctypes\__init__.py", line 434, in LoadLibrary
        return self._dlltype(name)
    File "DIR\Python37-32\lib\ctypes\__init__.py", line 356, in __init__
        self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application

2 个答案:

答案 0 :(得分:0)

欢迎使用StackOverflow!

不确定这是否是您看到的错误msg的原因,但是仅供参考,用于构建扩展程序的C ++编译器应与用于构建所使用的Python的编译器相同。您可以通过输入useDb来了解,Enthought的Python 3.5.2是使用 <div class="container-fluid dashboard"> <!--TITLE --> <div class="title container row"> <div class="col-sm-12"> <h1 class="heading">Your Dancers</h1> </div> </div> <div class="row container" style="margin: auto;"> <div class="col-sm-4"> <input type="text" id="myInput" placeholder="Search by name, age, level, etc.."> </div> <div class="col-sm-2"></div> <div class="col-sm-2 " style="text-align: right;"> <select class="level"> <option value="all">All Levels</option> <option value="Pre-Beginner"> Pre-Beginner (1)</option> <option value="Beginner"> Beginner (1)</option> <option value="Novice"> Novice (3)</option> <option value="Prize Winner"> Prize Winner (3)</option> <option value="Prelim Champion"> Prelim Champion (1)</option> <option value="Open Champion"> Open Champion (3)</option> </select> </div> <div class="col-sm-2 " style="text-align: right;"> <select class="gender"> <option value="all">All Genders</option> <option value="Female"> Female</option> <option value="Male"> Male</option> </select> </div> <div class="col-sm-2 " style="text-align: right;"> <select class="teachers"> <option value="all">All Teachers</option> <option value="Mike" > Mike</option> <option value="Mr. Alpha" > Mr. Alpha</option> <option value="Ms. Lee" > Ms. Lee</option> <option value="Liz" > Liz</option> </select> </div> </div> <!--DANCER GALLERY--> <div class="row container FilterContainer" style="margin: auto; margin-bottom: 2rem;"> <div class="col-sm-3 column" data-level="Novice" data-teacher="Liz" data-gender="Female"> <a href="dancerProfile?id=1" class="link"> <div class="content"> <img src="uploads/profile-icon.png" alt="Lace" style="width:80%; height: 170px;"> <h4>Lace - Novice</h4> <p>Age: 14, teacher: Liz</p> </div> </a> </div> <div class="col-sm-3 column" data-level="Novice" data-teacher="Mr. Alpha" data-gender="Female"> <a href="dancerProfile?id=3" class="link"> <div class="content"> <img src="uploads/1782517_575923289159969_1857743309_o.jpg" alt="Rhea" style="width:80%; height: 170px;"> <h4>Rhea - Novice</h4> <p>Age: 5, teacher: Mr. Alpha</p> </div> </a> </div> <div class="col-sm-3 column" data-level="Open Champion" data-teacher="Ms. Lee" data-gender="Female"> <a href="dancerProfile?id=48" class="link"> <div class="content"> <img src="imgs/profile-icon%20(1).png" alt="Isabelle" style="width:80%; height: 170px;"> <h4>Isabelle - Open Champion</h4> <p>Age: 15, teacher: Ms. Lee</p> </div> </a> </div> </div> </div> 参见https://stackoverflow.com/a/2676904/1988991

可能是您使用更高版本的编译器构建了DLL。由于它似乎可以在Python 3.7中使用,因此Visual Studio 2017或2019似乎很有可能。参见https://www.scivision.dev/python-windows-visual-c-14-required/

答案 1 :(得分:0)

原来,该DLL依赖于另一个DLL,并且该DLL是在python中自动找到的。但是,在Canopy中,第二个DLL需要单独加载。