Skip to content Skip to sidebar Skip to footer

Importing Dll Into Python 3 Without Imp.load_dynamic

Goal I am trying to add Windows support for Python Interface to Total Phase Aardvark that is currently Linux only . This is a wrapper for a device whose available interface is onl

Solution 1:

I had some success with importing an external library on Python 3.6 through importlib. The official docs give this recipe to import a source file directly:

import importlib.util
import sys

# For illustrative purposes.import tokenize
file_path = tokenize.__file__
module_name = tokenize.__name__

spec = importlib.util.spec_from_file_location(module_name, file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Optional; only necessary if you want to be able to import the module# by name later.
sys.modules[module_name] = module

(https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly)

I adapted this to:

import importlib.util

defload_dynamic(module, path):
    spec = importlib.util.spec_from_file_location(module, path)
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod

You should then be able to do something like this on Python 3:

load_dynamic('aardvark', os.path.join(cur_path, 'ext', 'win32', 'aardvark.dll'))

Post a Comment for "Importing Dll Into Python 3 Without Imp.load_dynamic"