not2qubit
not2qubit

Reputation: 16997

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

I'm trying to programmatically determine the path to the included/installed DLL file for a particular package. I've read countless SO pages, but cannot find any solution. Perhaps I've missed something and it's not possible?

The package is capstone and was manually installed from sources with the python3 bindings on Windows via Cygwin. Everything works.

# 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. The path shown above is to the *.egg file, but that path doesn't actually exist,
    unless you unzip the file.
  2. Within the EGG file, the location is in ./*.egg/capstone/lib/capstone.dll
  3. But in the OS, the real system location of capstone.dll is in:
    /usr/lib/python3.6/site-packages/capstone/lib

How can I get the true path (3) in Python3?


EDIT:

Perhaps this could be useful? But I've come up with this ugly thing, that could easily break, so hoping for a more pythonic way.

# 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

Upvotes: 4

Views: 3157

Answers (1)

CristiFati
CristiFati

Reputation: 41137

I've "installed" Capstone by copying:

  1. Python bindings dir ([GitHub]: capstone-engine/capstone - (master) capstone/bindings/python/capstone) in my CWD

  2. capstone.dll from the binaries .zip file, in the dir from #1.

While starting to prepare an elaborate (and general) example, I browsed a bit the source (as at the beginning, it didn't find the .dll - hence the need for setting ${LIBCAPSTONE_PATH}), and noticed that the .dll path is stored in capstone._path :)

Output:

[cfati@cfati-5510-0:/cygdrive/e/Work/Dev/StackOverflow/q052946558]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> ls
capstone  capstone-4.0-win64.zip  capstone-master.zip
[064bit prompt]> 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
[064bit prompt]>
[064bit prompt]> 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/StackOverflow/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.
>>>
[064bit prompt]>
[064bit prompt]> 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/StackOverflow/q052946558/capstone/capstone.dll'

Upvotes: 2

Related Questions