wolfpld / tracy

Frame profiler
https://tracy.nereid.pl/
Other
10.25k stars 692 forks source link

Python Tracy on Windows - ImportError: DLL load failed while importing TracyClientBindings #901

Open melnykov-photoneo opened 1 month ago

melnykov-photoneo commented 1 month ago

Hey there. I apologise in advance if my description is poor or if it's a stupid mistake on my part, I'm not overly familiar with CMake, neither am I with how dlls are being loaded (to be fixed)

I wanted to use Tracy to track a python application, and I needed to run it on Windows. So I followed the instructions from docs to build a python wheel.

$ cmake --version
cmake version 3.26.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ py -3.11 --version
Python 3.11.6

$ mkdir build && cd build
$ cmake -DTRACY_CLIENT_PYTHON=ON -DBUILD_SHARED_LIBS=ON ..
$ cmake --build . --config=Release

btw, at this point I had to manually move TracyClient.dll from Release folder one level above, because that's where python recipe was expecting it

$ cmake --build . --config=Release # (again, because of the error described above)
$ cd ../python
$ py -3.11 -m setup bdist_wheel

I successfully got a wheel for myself, and went on to pip install it

At this point, in my .venv/Lib/site-packages I had a tracy_client folder, all according to plan.

$ ls .venv/Lib/site-packages/tracy_client
TracyClient.dll*          TracyClientBindings.exp  TracyClientBindings.pyd*  __init__.py   py.typed   tracy.py
TracyClientBindings.dll*  TracyClientBindings.lib  TracyClientBindings.pyi   __pycache__/  scoped.py

So I went on and added a import tracy_client as Tracy to my source. However, when running the code, I've received (<class 'ImportError'>, DLL load failed while importing TracyClientBindings: The specified module could not be found., <traceback object at 0x000001CFCFE55D80>) in tracy_client.tracy.py (I imagine because it failed to find TracyClientBindings.pyd)

wolfpld commented 1 month ago

@Chekov2k

Chekov2k commented 1 month ago

Morning, I'm afraid my experience with compiling anything on Windows is basically non-existent :-( Does it work if you tell Windows where to find TracyClientBindings.pyd?

melnykov-photoneo commented 1 month ago

Hi. From what I read, PYTHONPATH could help with that and I did setup it to point to the folder where the .pyd is supposed to be, tried to sys.path.append() the said folder, went so far as to add the said pyd to C:\Windows\Sys32/SysWOW64 (which is one of default locations on PATH I believe), but nothing seems to have worked for me so far

Chekov2k commented 1 month ago

Since it's complaining about DLL loads, maybe something like this might help? https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order

melnykov-photoneo commented 1 month ago

Hi there. Status update: sadly, I wasn't able to arrive to a solution. However I said "whatever", kinda migrated my app to Ubuntu and built Tracy for Python, everything worked without issues This could be a "me-specific" issue... But I have no way of confirming it. In any case, I thank you for your help!

P.S. I'm not sure if this issue can be closed or if you'd like to look into it further, so I'll leave it up to you🙂

Zekezero commented 1 week ago

image I have the same problem, maybe 'tracy_client.TracyClientBindings‘ was not correct build by wheel? It's no problem to import in buiding pacakge but fail outside

Zekezero commented 1 week ago

image Maybe need copy lib and dll to site-packages

melnykov-photoneo commented 1 week ago

Maybe need copy lib and dll to site-packages

Tha's the thing, they are actually present in my wheel (see the original msg by yours truly), but python seemingly fails to locate the .dll. I've tried to shove said dll down many... unique places you could say, but nothing seems to have helped

P.S. it actually resolves the stub, and I can navigate to it in IDE just fine, the trouble comes specifically when loading the DLL, because it's "missing"

Chekov2k commented 1 week ago

Does something like this os.add_dll_directory("${path_to_working_dlls_directoy}") work (see https://stackoverflow.com/questions/59330863/cant-import-dll-module-in-python)?