如何找到 python 模块包含的 DLL 文件的完整路径?

How to find the full path to a python module's included DLL file?

我正在尝试以编程方式确定特定程序包的 included/installed DLL 文件的路径。我已经阅读了无数 SO 页面,但找不到任何解决方案。也许我错过了什么,这是不可能的?

该软件包是 capstone,是通过 Cygwin 从 python3 绑定到 Windows 的来源手动安装的。一切正常。

# python3 -c "import os,inspect,capstone; print(os.path.dirname(inspect.getfile(capstone)))"
/usr/lib/python3.6/site-packages/capstone-4.0.0rc1-py3.6.egg/capstone

# python3 -c "import capstone; print(capstone._lib)"
capstone.dll
  1. 上面显示的路径是 *.egg 文件,但该路径实际上并不存在,
    除非你解压文件。
  2. 在 EGG 文件中,位置在 ./*.egg/capstone/lib/capstone.dll
  3. 但在OS中,capstone.dll的真实系统位置在:
    /usr/lib/python3.6/site-packages/capstone/lib

如何获得 Python3 中的真实路径 (3)?


编辑:

也许 this 有用?但是我想出了这个丑陋的东西,它很容易坏掉,所以希望有一种更 pythonic 的方式。

# python3 -c "import capstone; print('DLL path: %s' % capstone._path_list[4] + '/' + capstone.__name__ + '/lib/' + capstone._lib)"
DLL path: /usr/lib/python3.6/site-packages/capstone/lib/capstone.dll

我已经 "installed" capstone 通过复制:

  1. Python 绑定目录 ([GitHub]: aquynh/capstone - (master) capstone/bindings/python/capstone) 在我的 cwd
  2. capstone.dll 来自二进制 .zip 文件,位于 [=20= 的目录中]#1.

在开始准备一个详细的(和一般的)示例时,我浏览了一下源代码(因为在开始时,它没有找到 .dll - 因此需要设置 ${LIBCAPSTONE_PATH}),并注意到 .dll 路径存储在 capstone._path :)

输出:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q052946558]> ls
capstone  capstone-4.0-win64.zip  capstone-master.zip
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q052946558]> ls capstone
__init__.py   __pycache__  arm_const.py  arm64_const.py  evm.py        m680x.py        m68k.py        mips.py        ppc.py        sparc.py        systemz.py     tms320c64x.py        x86.py        xcore.py
__init__.pyc  arm.py       arm64.py      capstone.dll    evm_const.py  m680x_const.py  m68k_const.py  mips_const.py  ppc_const.py  sparc_const.py  sysz_const.py  tms320c64x_const.py  x86_const.py  xcore_const.py
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q052946558]>
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q052946558]> python3
Python 3.6.4 (default, Jan  7 2018, 15:53:53)
[GCC 6.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import capstone
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/cygdrive/e/Work/Dev/Whosebug/q052946558/capstone/__init__.py", line 315, in <module>
    raise ImportError("ERROR: fail to load the dynamic library.")
ImportError: ERROR: fail to load the dynamic library.
>>>
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q052946558]>
[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/Whosebug/q052946558]> LIBCAPSTONE_PATH=$(pwd)/capstone python3
Python 3.6.4 (default, Jan  7 2018, 15:53:53)
[GCC 6.4.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import capstone
>>> import os
>>> os.path.join(capstone._path, capstone._lib)
'/cygdrive/e/Work/Dev/Whosebug/q052946558/capstone/capstone.dll'