mhammond / pywin32

Python for Windows (pywin32) Extensions
4.99k stars 790 forks source link

ImportError: DLL load failed: The specified module could not be found #1183

Closed morsedl closed 6 months ago

morsedl commented 6 years ago

Hi,

I need to install PyWin32 for an application that includes a framework for writing plugins in Python (Cinema 4D, specifically; henceforth C4D), that is, has a Python interpreter shipped with it. I've had no troubles installing many other modules under this framework (e.g., SQLAlchemy), but PyWin32 is giving me fits.

When I install Python under C:\Python27 and run either the executable installer for PyWin32 or "Scripts\pip install pywin32", I can then "import win32security" with that python interpreter without trouble. Thus, in principle, PyWin32 has no problems functioning correctly on my machine.

However, when I copy the files installed under site-packages to the site-packages directory of C4D, I always get the DLL load failed error. I've ensured that Windows' PATH variable includes the pywin32_system32 directory, and even tried copying all the DLLs from the PyWin32 directories under site-packages to both \Windows\System32 and \Windows\SysWOW64, but the problem persists. I've even tried copying the entire site-packages directory to a different location and the used "import site" with the appropriate calls so that the .pth file is used --- which I've checked to ensure that it is indeed being used and used correctly (i.e., win32, win32\lib, and pythonwin are added to sys.path and at the OS level pywin32_system32 is added to the PATH) --- but some DLL is still not loading. I've also tried installing pip under C4D's python and then running "pip install pywin32", which installs fine and without error, but still some DLL is not loading. Finally, I've made these attempts both with and without python installed in C:\Python27, both with and without PyWin32 installed from the executable installer, and all the possible combinations of these.

So, I'm at a bit of a loss. If anyone has any ideas, I'd be most grateful to hear them.

All I really need is win32security so that I can temporarily use Impersonation, as for security reasons part of my Python plugin must copy some files to a network share as a user other than the logged-in user. If there are other ways to accomplish this, with or without PyWin32, I'd also be glad to hear about them.

If I could determine which DLL is not loading, that of course would be helpful. I do not understand at all why the error message does not clarify which DLL cannot load, but my understanding is that this failure to report better information is rooted in Windows itself rather than Python or PyWin32. I've of course considered using some utility for determining which DLLs are not loading, but I'm not sure how I would use such a tool when Python itself is running within C4D. Again, any pointers or suggestions would be most welcome.

Thanks, Doug

mhammond commented 6 years ago

It may be that C4D is modifying the path - however, if you installed pywin32 via the executable, it should have copied the DLLs it needs into the System32 directory. Does "import pywintypes" work? You could also try the http://www.dependencywalker.com/ tool and see if that helps you locate the issue.

morsedl commented 6 years ago

@mhammond Thanks for the reply. Yeah, I've already tried that. C4D isn't changing the system path, and as stated I've even copied all the DLLs in every PyWin32 directory and subdirectory into a common directory and added it to the Windows global system PATH. No joy. Dependency Walker showed python27.dll and msvcrt90.dll missing, but that went away when I copied them into pywin32system32. It also showed a bunch of api*.dll missing, which are not actual dlls from my understanding, and I couldn't figure out how to resolve those. It also noted a mixture of 32-bit and 64-bit DLLs. Both of these last two issues however are know to be at times Dependency Walker false alarms. Of course this was all done outside of the context of a running C4D (not sure, I guess I should try: Can one pass an .exe to DLL?; the DLL I tested was one I discovered definitely had missing dependencies under C4D, pywintypes.dll; can't recall how I discovered that).

It's worth noting that the exact same dependency problems appeared when I used DW to inspect under c:\python27\Lib\site-packages..., where "import win32security" succeeds without error.

All very confusing. Stupid Windows for not reporting which DLL it couldn't load. It's really astounding that after nearly three decades "DLL hell" still exists.

morsedl commented 6 years ago

... passed an .exe to Dependency Walker... I meant

ksmyth commented 6 years ago

The best way to debug this (your path to Debugging Tools may vary):

"C:\Program Files (x86)\Debugging Tools for Windows (x86)\gflags.exe"  -i C4D.exe +sls
"C:\Program Files (x86)\Debugging Tools for Windows (x86)\cdb.exe"  -g -G -logo debuglog.txt C4D.exe

Then grep through the very verbose debuglog.txt for ERROR: or 0xc0000135

mhammond commented 6 years ago

Dependency Walker showed python27.dll and msvcrt90.dll missing,

Copying python27.dll into pywin32_system32 sounds like the wrong thing to do. You are probably best off copying the files in that directory to wherever python27.dll is. I'm not sure what pywin32 can do to support these kinds of custom installations.

Avasam commented 6 months ago

Since 71afa71e11e6631be611ca5cb57cda526b5e91ab, the PATH is now prepended, rather than appended. For Python >= 3.8, the PATH environment variable isn't even used. This is included in pywin32==226.

Python 3.7 has also reached EOL (meaning no security fixes/support) and I'd recommend migrating over to 3.8+ (see #2207)

.exe installers are now deprecated #1939, so you should install from PyPI (ie: pip install pywin32>=226)

Given the sheer amount of "ImportError: DLL load failed: The specified module could not be found." issue duplication and old history, I'd like to consolidate the current state of these issues. So I'll close and reference back any issue that only has comments showing the issue:

Please also try https://github.com/mhammond/pywin32#troubleshooting before opening a new issue, or let us, and others, know if it did resolve your issue.