python cdll找不到模块 [英] python cdll can't find module

查看:762
本文介绍了python cdll找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由两个dll文件和一个python包装器组成的库。

I have a library consisting of two dll files and one python wrapper.

我目前有基于这三个文件的代码,它们与我的主文件位于同一父目录中python文件。我现在正在尝试重构内容,然后继续开发,并希望将所述库代码移至其自己的lib /目录中。不幸的是,我尝试过的所有方法都无济于事。

I currently have code based on these three files being in the same parent directory as my main python file. I am now attempting to refactor things before I continue development and would like to move said library code into it's own lib/ directory. Unfortunately, nothing I've tried helps.

import ctypes

_lib = ctypes.cdll["./my.dll"]

上面位于python包装文件中的代码将dll加载得非常好在它的原始位置。我尝试了各种在新位置加载它的方法,例如:

The above code located in the python wrapper file loads the dll perfectly fine in it's original location. I've tried various ways of loading it in the new location such as:

from ctypes import *
import os

path = os.path.dirname(os.path.realpath(__file__))
_lib = ctypes.CDLL(os.path.join(path, 'my.dll'))

但是python总是抛出一个异常,提示无法找到模块。.我复制并粘贴了路径验证它实际上是.dll文件的有效绝对路径

However python always throws an exception saying unable to find the module.. I have copy and pasted the path to verify that it is in fact the valid absolute path to the .dll file

有人知道我需要执行什么操作才能将该库重新定位到子文件夹吗?我总是可以将其保留在原处,但我只是讨厌混乱。

Does anyone know what I need to do in order to relocate this library to a sub folder? I could always leave it where it is but I simply hate clutter.

推荐答案

我在尝试加载<$时遇到了相同的问题c $ c> magic1.dll -该文件依赖于其他两个.dll,并且当我从当前工作目录中移动magic1.dll时-无法加载。

I had the same issue with trying to load magic1.dll - this file depends on two other .dll's, and when I moved magic1.dll from my current working dir - I could not load.

此解决方法有所帮助:

pathToWin32Environment = os.getcwd() + "/environment-win32/libmagic/"
pathToDll = pathToWin32Environment + "magic1.dll"
if not os.path.exists(pathToDll):
    #Give up if none of the above succeeded:
    raise Exception('Could not locate ' + pathToDll)
curr_dir_before = os.getcwd()
os.chdir(pathToWin32Environment)
libmagic = ctypes.CDLL('magic1.dll')
os.chdir(curr_dir_before)

这篇关于python cdll找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆