`import win32api` fails after `pip install pywin32` (ImportError: DLL load failed: The specified module could not be found)
Expected behavior and actual behavior.
Expected import win32api to work without any exception.
Steps to reproduce the problem.
Below I am working in a virtualenv; however, this same problem occurs when I install not inside a venv (I just solved it for my system, so it's easier to illustrate inside a venv now).
pip install pywin32
python -c "import win32api"
With more verbosity, my steps look like:
(pywin32test) C:\Users\WolfgangRichter\Desktop>pip install pywin32
Collecting pywin32
Using cached https://files.pythonhosted.org/packages/a3/8a/eada1e7990202cd27e58eca2a278c344fef190759bbdc8f8f0eb6abeca9c/pywin32-224-cp37-cp37m-win_amd64.whl
Installing collected packages: pywin32
Successfully installed pywin32-224
(pywin32test) C:\Users\WolfgangRichter\Desktop>python -c "import win32api"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
Version of Python and pywin32
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:13:57) [MSC v.1916 64 bit (AMD64)] on win32
pywin32-224-cp37-cp37m-win_amd64.whl
I was able to resolve this by copying two DLLs:
(pywin32test) C:\Users\WolfgangRichter\Desktop>copy C:\Users\WolfgangRichter\Desktop\pywin32test\Lib\site-packages\pywin32_system32\pythoncom37.dll C:\Users\WolfgangRichter\Desktop\pywin32test\Lib\site-packages\win32\
1 file(s) copied.
(pywin32test) C:\Users\WolfgangRichter\Desktop>copy C:\Users\WolfgangRichter\Desktop\pywin32test\Lib\site-packages\pywin32_system32\pywintypes37.dll C:\Users\WolfgangRichter\Desktop\pywin32test\Lib\site-packages\win32\
1 file(s) copied.
(pywin32test) C:\Users\WolfgangRichter\Desktop>python -c "import win32api"
(pywin32test) C:\Users\WolfgangRichter\Desktop>
But, ideally, this is not required on a fresh install.
Hello @theonewolf,
could you be so kind and run Python in your venv again and run the following lines?
import os
os.environ["PATH"]
Normally our .pth file within the site-packages directory should ensure that the .dll files can be found. However, due to your venv setup, it seems that something is mixed up there.
Thanks!
Is it possible you have either a pythoncomXX.dll or pywintypesXX.dll in your system32 directory?
I have the same problem. Have you solved it?
I had the same problem, but with version 225. Version 224, on the other hand, worked just fine for me.
I was able to fix this by removing the files pythoncom27.dll, pythoncomloader27.dll and pywintypes27.dll from C:\Windows\SysWOW64. They seemed to be quite old (2016-01-11 from 22:44 to 22:51), possibly from an earlier installation of the package (and not via pip?).
I could find the current versions of the DLLs in C:\Python27\lib\site-packages\pywin32_system32, which indeed is listed as the last entry of python -c "import os; print os.environ['PATH']".
So, thanks for your tips, everyone.
Possibly this issue is related to the issues #1406 and #1409.
I just installed 32-bit Python 3.8b04 on Windows and then tried
pip install -I pywin32-225-cp38-cp38-win32.whl
and ended up needing to copy the DLLs from Python38-32\Lib\site-packages\pywin32_system32 to my embedding binary's root folder.
Where are these binaries
09/26/2019 09:56 PM 423,424 pythoncom38.dll
09/26/2019 09:56 PM 113,664 pywintypes38.dll
supposed to go when one runs
pip install -I pywin32-225-cp38-cp38-win32.whl
For me the issue had been caused by old versions of pythoncom27.dll, pythoncomloader27.dll and pywintypes27.dll somewhere in the PATH before the ...\lib\site-packages\pywin32_system32. (See my comment above.) Thus Windows finds those old versions first, and they seem to have an incompatible public interface (exported symbols), hence the DLL load error.
Possibly the issue can be solved by prepending the pywin32_system32 instead of appending it, in the file pywin32.pth:
os.environ["PATH"]=('' if pywin32_system32 in os.environ["PATH"] else (pywin32_system32+";"))+os.environ["PATH"]
pywin32_postinstall.py -install fixed it for me
Is it possible you have either a pythoncomXX.dll or pywintypesXX.dll in your system32 directory?
I copy this two dll into my system32 directory, but error still appear. also I add pywin32_system32 in my system environment varialble, but still did not work.
I installed python 3.7.4 in my wide system and import win32com works for me, but i want to able to run it from another location. thanks.
my python is:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
i copy the pythoncom37.dll pywintypes37.dll to C:\Windows\System32
but not work for me.
there are error: ImportError: DLL load failed: %1 Win32 。
@azoellner 's suggestion of prepending the path has been done in 71afa71e11e6631be611ca5cb57cda526b5e91ab for Python versions older than 3.8. 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:
- from before November 10th, 2019 (pywin32<226)
- using
.exeinstallers - using Python < 3.8
Please also try https://github.com/mhammond/pywin32#troubleshooting before opening a new issue, or let us, and others, know here if it did fix your issue.
If you're having this issue with Jupyter Notebooks, refer to https://github.com/mhammond/pywin32/issues/1409 instead. If you're having this issue using the Microsoft Store distribution of Python, refer to https://github.com/mhammond/pywin32/issues/1383 instead. If you're having this issue with the Anaconda distribution of Python, refer to https://github.com/mhammond/pywin32/issues/1865 instead.