How to fix "ImportError: DLL load failed" while importing win32api

113,749

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

Share:
113,749
Admin
Author by

Admin

Updated on February 13, 2022

Comments

  • Admin
    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
    Admin over 4 years
    thank you but unluckily nothing changes.. here's what i did Successfully uninstalled pypiwin32-223 C:\Users\gfroz\AppData\Local\Programs\Python\Python38-32\Scr‌​ipts>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
    phd over 4 years
    Try to reinstall it; after all your experiments with different packages the DLLs could be broken: pip install --ignore-installed pywin32
  • Admin
    Admin over 4 years
    thank 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 tried import win32api and no error appear!
  • Sabito 錆兎 stands with Ukraine
    Sabito 錆兎 stands with Ukraine about 4 years
    where is this Lib\site-packages\pywin32_system32 path?
  • gaborous
    gaborous almost 4 years
    To run pywin32_postinstall.py, you need to python pywin32_postinstall.py -install, else you'll get an importerror about winreg
  • Lynn Han
    Lynn Han almost 4 years
    It worked for my issue. I use Pyhon3.8 either. I want to know why it happens?
  • Guido
    Guido almost 4 years
    I get this error under Python3.8: Could not find a version that satisfies the requirement pywin32==224 (from versions: 225, 226, 227, 228)
  • Guido
    Guido almost 4 years
    Same issue under Python 3.8. Solution works, but I had to take the files under my virtual environment Lib folder
  • Abhimanu Kumar
    Abhimanu Kumar almost 4 years
    For python 3.8, if you cannt find pywin32==224, try pywin32==225. It worked for me.
  • rushikesh.meharwade
    rushikesh.meharwade almost 4 years
    This one actually helped me. I guess the problem is with Python 3.8 version.
  • Cédric Dromzée
    Cédric Dromzée almost 4 years
    @Guido You must activate the base environment conda activate base
  • Robse
    Robse almost 4 years
    This helped me for 3.7 also.
  • ocroquette
    ocroquette almost 4 years
    This 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
    vlad over 3 years
    All 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
    Johannes Schmidt over 3 years
    You 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
    JoyfulPanda over 3 years
    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).
  • pansila
    pansila over 3 years
    I 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
    ocroquette over 3 years
    Maybe 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
    Binu over 3 years
    pip install --upgrade pywin32==225 worked for me..Thx
  • Johannes Schmidt
    Johannes Schmidt about 3 years
    Thanks, this solved the issue for me. I'm using miniconda.
  • Ismael EL ATIFI
    Ismael EL ATIFI about 3 years
    Worked for me on Win10, Python 3.9 and Miniforge.
  • swimmer
    swimmer almost 3 years
    pip install pywin32==225 also works for Python 3.9
  • wtfzambo
    wtfzambo almost 3 years
    @Binu solution worked for me too as of 2021-06-04
  • Shaida Muhammad
    Shaida Muhammad almost 3 years
    Worked for me in python 3.9.5
  • Johannes
    Johannes almost 3 years
    for me a similar approach worked. I downgraded from 227 to 226. also a newer version like 301 did not workout for me.
  • Shravya Mutyapu
    Shravya Mutyapu almost 3 years
    Worked perfectly for jupyter notebook as well. Thanks.
  • Piotr Migdal
    Piotr Migdal almost 3 years
    I had to install it within a given conda environment (rather than in one I run jupyter notebook from). Nonetheless, it works!
  • Julian Moore
    Julian Moore almost 3 years
    We 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
    Oscar Chen over 2 years
    Thanks, pip install pywin32==225 worked for me on Windows, Python 3.8
  • Kris Stern
    Kris Stern over 2 years
    I think this is the most orthodox solution amongst all available solutions. It works because extra pywin32 extensions need to be installed.
  • tripleee
    tripleee over 2 years
    Thanks for the background. In addition, I had to downgrade to version 224.
  • JimB
    JimB over 2 years
    At an admin command prompt, go to python's Scripts dir and run python -B pywin32_postinstall.py -install BUT if you have multiple venv'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
    Nir over 2 years
    As 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
    George Ogden over 2 years
    In general for python 3.8, you can use: pip install pywin32
  • Laurent T
    Laurent T over 2 years
    pip install pywin32==228 works perfectly for me
  • user3761555
    user3761555 about 2 years
    C:\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
    Arash about 2 years
    None of the suggested solutions solved this issue for me, but yours solved it for me. Thanks heaps.
  • Ian Thompson
    Ian Thompson almost 2 years
    link to the answer on the issue