How to fix "ImportError: DLL load failed" while importing win32api
Solution 1
Run Scripts\pywin32_postinstall.py -install in an Admin command prompt
ref: https://github.com/mhammond/pywin32/issues/1431
edit: User @JoyfulPanda gave a warning:
Running this script with admin rights will also copy pythoncom37.dll, pywintypes37.dll (corresponding to the pywin32 version), into
C:\WINDOWS\system32
, which effectively overwrites the corresponding DLL versions from Anaconda already there. This later causes problem when openning (on Windows) "Start Menu > Anaconda3 (64-bit) > Anaconda Prompt (a_virtual_env_name)". At least Anaconda 2019.07 has pywin32 223 installed by default. Pywin32 224 may work, but 225-228 causes problem for Anaconda (2019.07)
Solution 2
Solved
If you are working in a miniconda on conda environment. You could just install pywin32 using conda instead of pip.
This solved my problem:
conda install pywin32
Solution 3
For my case, install and reinstall pywin32 doesn't help. After copied the two files from [installation directory of Anaconda]\Lib\site-packages\pywin32_system32
to C:\Windows\System32
, it works.
My environment is python 3.8 in miniconda. The two files are pythoncom38.dll
and pywintypes38.dll
.
Solution 4
For me, it worked by downgrading my pywin32 from version 227 to version 224. Just type the following command on any shell in administrator mode:
pip install --upgrade pywin32==224
Solution 5
The answer is in jupyter notebook github. https://github.com/jupyter/notebook/issues/4980
conda install pywin32
worked for me. I am using conda distribution and my virtual env is using Python 3.8
Admin
Updated on February 13, 2022Comments
-
Admin over 2 years
I'm setting up an autoclicker in Python 3.8 and I need win32api for GetAsyncKeyState but it always gives me this error:
>>> import win32api Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: DLL load failed while importing win32api: The specified module could not be found.
I'm on Windows 10 Home 64x. I've already tried
pip install pypiwin32
And it successfully installs but nothing changes. I tried uninstalling and re-installing python as well. I also tried installing 'django' in the same way and it actually works when I
import django
, so I think it's a win32api issue only.>>> import win32api
I expect the output to be none, but the actual output is always that error ^^
-
Admin over 4 yearsthank you but unluckily nothing changes.. here's what i did
Successfully uninstalled pypiwin32-223 C:\Users\gfroz\AppData\Local\Programs\Python\Python38-32\Scripts>pip install pywin32 Requirement already satisfied: pywin32 in c:\users\gfroz\appdata\local\programs\python\python38-32\lib\site-packages (225)
and then>>> import win32api Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: DLL load failed while importing win32api: Impossibile trovare il modulo specificato.
-
phd over 4 yearsTry to reinstall it; after all your experiments with different packages the DLLs could be broken:
pip install --ignore-installed pywin32
-
Admin over 4 yearsthank you so much! actually it didn't work first when i just installed pywin32 but then i wrote in the scripts folder
pywin32_postinstall.py -install
, then i triedimport win32api
and no error appear! -
Sabito 錆兎 stands with Ukraine about 4 yearswhere is this
Lib\site-packages\pywin32_system32
path? -
gaborous almost 4 yearsTo run pywin32_postinstall.py, you need to
python pywin32_postinstall.py -install
, else you'll get an importerror about winreg -
Lynn Han almost 4 yearsIt worked for my issue. I use Pyhon3.8 either. I want to know why it happens?
-
Guido almost 4 yearsI get this error under Python3.8: Could not find a version that satisfies the requirement pywin32==224 (from versions: 225, 226, 227, 228)
-
Guido almost 4 yearsSame issue under Python 3.8. Solution works, but I had to take the files under my virtual environment
Lib
folder -
Abhimanu Kumar almost 4 yearsFor python 3.8, if you cannt find pywin32==224, try pywin32==225. It worked for me.
-
rushikesh.meharwade almost 4 yearsThis one actually helped me. I guess the problem is with Python 3.8 version.
-
Cédric Dromzée almost 4 years@Guido You must activate the base environment
conda activate base
-
Robse almost 4 yearsThis helped me for 3.7 also.
-
ocroquette almost 4 yearsThis information helped, but putting 3rd party stuff in C:\Windows\System32 is a recipe for future trouble. Think for instance what will happen if pywin32 updates the DLL and you don't copy them again to C:\Windows\System32 after a pip update. You will get an inconsistent system. In addition, all programs of the computer will "see" these DLLs. You should rather set the PATH as/where required.
-
vlad over 3 yearsAll pywin32_postinstall.py doing is copying pywintypes[pyton version].dll and pythoncom[pyver].dll from \Lib\site-packages\pywin32_system32\ into main dir. Actually better to just manually copy to \Library\bin. Then it all works.
-
Johannes Schmidt over 3 yearsYou must not activate the base env, only the env that you want to use. Downgrading from 228 to 225 worked for me.
pip install --upgrade pywin32==225 worked
-
JoyfulPanda over 3 yearsRunning this script with admin rights will also copy
pythoncom37.dll
,pywintypes37.dll
(corresponding to the pywin32 version), into `C:\WINDOWS\system32`, which effectively overwrites the corresponding DLL versions from Anaconda already there. This later causes problem when openning (on Windows) "Start Menu > Anaconda3 (64-bit) > Anaconda Prompt (a_virtual_env_name)". At least Anaconda 2019.07 has pywin32 223 installed by default. Pywin32 224 may work, but 225-228 causes problem for Anaconda (2019.07). -
pansila over 3 yearsI have added absolute path of 'pywin32_system32' to the os.environ['PATH'] and still can't import win32api. Only copying the two dlls to the system32 could work.
-
ocroquette over 3 yearsMaybe because of this change in 3.8: DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution.
-
Binu over 3 yearspip install --upgrade pywin32==225 worked for me..Thx
-
Johannes Schmidt about 3 yearsThanks, this solved the issue for me. I'm using miniconda.
-
Ismael EL ATIFI about 3 yearsWorked for me on Win10, Python 3.9 and Miniforge.
-
swimmer almost 3 years
pip install pywin32==225
also works for Python 3.9 -
wtfzambo almost 3 years@Binu solution worked for me too as of 2021-06-04
-
Shaida Muhammad almost 3 yearsWorked for me in python 3.9.5
-
Johannes almost 3 yearsfor me a similar approach worked. I downgraded from 227 to 226. also a newer version like 301 did not workout for me.
-
Shravya Mutyapu almost 3 yearsWorked perfectly for jupyter notebook as well. Thanks.
-
Piotr Migdal almost 3 yearsI had to install it within a given conda environment (rather than in one I run jupyter notebook from). Nonetheless, it works!
-
Julian Moore almost 3 yearsWe shall hold a festival in your name! Win 10 64-bit. Hours wasted... but thanks to this, issue solved. conda install pywin32 in my activated env didn't affect system32 and it avoided the need to run the postinstall script which failed because wintypes DLL was not found. It shouldn't be possible to get into this ridiculous state (I don't know how it happened) and it should not take this much work to get out. But thank you thank you thank you.
-
Oscar Chen over 2 yearsThanks,
pip install pywin32==225
worked for me on Windows, Python 3.8 -
Kris Stern over 2 yearsI think this is the most orthodox solution amongst all available solutions. It works because extra
pywin32
extensions need to be installed. -
tripleee over 2 yearsThanks for the background. In addition, I had to downgrade to version 224.
-
JimB over 2 yearsAt an admin command prompt, go to python's
Scripts
dir and runpython -B pywin32_postinstall.py -install
BUT if you have multiplevenv
's, they better have the same version of pywin32 or the Import Error will come back when you switch (pip list
to check). See this answer. -
Nir over 2 yearsAs of November 2021,
pip install --upgrade pywin32==300
works, pwin32 versions 301, 302 fail! [SO community members should get paid for their QA services :)] -
George Ogden over 2 yearsIn general for python 3.8, you can use:
pip install pywin32
-
Laurent T over 2 yearspip install pywin32==228 works perfectly for me
-
user3761555 about 2 yearsC:\ProgramData\Anaconda3>py Scripts\pywin32_postinstall.py -install ...Pythonwin has been registered in context menu Creating directory C:\ProgramData\Anaconda3\Lib\site-packages\win32com\gen_py Can't install shortcuts - 'C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Python 3.8' is not a folder The pywin32 extensions were successfully installed.
-
Arash about 2 yearsNone of the suggested solutions solved this issue for me, but yours solved it for me. Thanks heaps.
-
Ian Thompson almost 2 years