Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python cdll can't find module

Tags:

python

ctypes

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

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"]

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'))

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.

like image 994
jdsmith2816 Avatar asked Jul 25 '26 12:07

jdsmith2816


1 Answers

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.

This workaround helped:

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)
like image 114
espenalb Avatar answered Jul 28 '26 01:07

espenalb