Importing DLL into Python 3 without imp.load_dynamic

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

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 only a .so (Linux) or .dll (Windows) closed source binary. However, it was made as a Python package (not sure if that is the right term) rather than just standard C interface that your would load with ctypes.

File Structure

In this project, we have an ext folder that is the same level as the script doing the importing, with 32/64 libraries for Linux and Windows (added by me):

pyaardvark.py (file doing imports) ext   linux32     __init.py__ (empty)     aardvark.so   linux 64     __init.py__ (empty)     aardvark.so   win32     __init.py__ (empty)     aardvark.dll   win64     __init.py__ (empty)     aardvark.dll 

Import Issues

Linux implementation uses:

from .ext.linux32 import aardvark as api 

I initially testing adding the windows import in Python 2.7 and was unable to get the relative path style import to work. The only method I was successful with is the following:

import imp import os cur_path = os.path.dirname(__file__) api = imp.load_dynamic('aardvark', os.path.join(cur_path, 'ext', 'win32', 'aardvark.dll')) 

This seems ugly, but works fine with Python 2.7 and all of the api is available.

I toggle over to Python 3.4 to test there and see that imp is deprecated. Not only that, it doesn't look like imp had load_dynamic in Python 3 from 3.2 on. I cannot find a way in Python 3.4 to make this DLL available.

Attempts

Method 1

Fails in both Python 2.7 and Python 3.4

from .ext.win32 import aardvark as api 

Method 2

Fails in both Python 2.7 and Python 3.4

import importlib import os cur_path = os.path.dirname(__file__) api = importlib.import_module('aardvark', os.path.join(cur_path, 'ext', 'win32', 'aardvark.dll')) 

Method 3

Works in Python 2.7, fails in Python 3.4

import imp import os cur_path = os.path.dirname(__file__) api = imp.load_dynamic('aardvark', os.path.join(cur_path, 'ext', 'win32', 'aardvark.dll')) 

Method 4

Doesn't throw an error in Python 2.7 or Python 3.4, but not what I'm looking for

import os cur_path = os.path.dirname(__file__) from ctypes import cdll api = cdll.LoadLibrary(os.path.join(cur_path, 'ext', 'win32', 'aardvark.dll')) 

With working (imp.load_dynamic) import in Python 2.7, dir(api) gives me:

['__doc__', '__file__', '__name__', '__package__', 'py_aa_async_poll', 'py_aa_close', 'py_aa_configure', 'py_aa_features', 'py_aa_find_devices', 'py_aa_find_devices_ext', 'py_aa_gpio_change', 'py_aa_gpio_direction', 'py_aa_gpio_get', 'py_aa_gpio_pullup', 'py_aa_gpio_set', 'py_aa_i2c_bitrate', 'py_aa_i2c_bus_timeout', 'py_aa_i2c_free_bus', 'py_aa_i2c_monitor_disable', 'py_aa_i2c_monitor_enable', 'py_aa_i2c_monitor_read', 'py_aa_i2c_pullup', 'py_aa_i2c_read', 'py_aa_i2c_read_ext', 'py_aa_i2c_slave_disable', 'py_aa_i2c_slave_enable', 'py_aa_i2c_slave_read', 'py_aa_i2c_slave_read_ext', 'py_aa_i2c_slave_set_response', 'py_aa_i2c_slave_write_stats', 'py_aa_i2c_slave_write_stats_ext', 'py_aa_i2c_write', 'py_aa_i2c_write_ext', 'py_aa_i2c_write_read', 'py_aa_log', 'py_aa_open', 'py_aa_open_ext', 'py_aa_port', 'py_aa_sleep_ms', 'py_aa_spi_bitrate', 'py_aa_spi_configure', 'py_aa_spi_master_ss_polarity', 'py_aa_spi_slave_disable', 'py_aa_spi_slave_enable', 'py_aa_spi_slave_read', 'py_aa_spi_slave_set_response', 'py_aa_spi_write', 'py_aa_status_string', 'py_aa_target_power', 'py_aa_unique_id', 'py_aa_version', 'py_version']

ctypes import dir(api) gives me:

['_FuncPtr', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_func_flags_', '_func_restype_', '_handle', '_name']

I'm not sure what to try next. I'm fine working with my stuff in Python 2.7, but I'd really like to offer Python 3 compatibility.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!