30

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 ^^

Gino Mempin
  • 12,644
  • 19
  • 50
  • 69
Glxce
  • 311
  • 1
  • 3
  • 5

13 Answers13

40

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.

Gjanetta
  • 37
  • 8
Bench Wang
  • 544
  • 1
  • 4
  • 3
  • where is this ```Lib\site-packages\pywin32_system32``` path? – Yatin May 15 '20 at 07:01
  • It worked for my issue. I use Pyhon3.8 either. I want to know why it happens? – Lynn Han May 22 '20 at 01:56
  • Same issue under Python 3.8. Solution works, but I had to take the files under my virtual environment `Lib` folder – Guido Jun 18 '20 at 12:45
  • This one actually helped me. I guess the problem is with Python 3.8 version. – rushikesh.meharwade Jul 12 '20 at 14:21
  • This helped me for 3.7 also. – Robse Jul 24 '20 at 09:45
  • 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. – ocroquette Jul 26 '20 at 16:43
35

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

Janzaib Masood
  • 351
  • 2
  • 4
20

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)

José
  • 987
  • 1
  • 10
  • 18
  • 2
    To run pywin32_postinstall.py, you need to `python pywin32_postinstall.py -install`, else you'll get an importerror about winreg – gaborous May 17 '20 at 07:51
  • 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). – JoyfulPanda Nov 12 '20 at 17:31
  • 10
    pip install --upgrade pywin32==225 worked for me..Thx – Binu Nov 27 '20 at 11:13
19

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
Jaroslav Bezděk
  • 2,697
  • 2
  • 14
  • 29
Aditya Saini
  • 405
  • 3
  • 12
10

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

vinodhraj
  • 111
  • 1
  • 6
6

This happens when Lib\site-packages\pywin32_system32 is not in the list of directories to search for DLL (PATH environment variable).

pywin32 (or one of its dependencies) adds this path at runtime to the PATH variable. If this is failing, or another component is overriding the PATH after it's been set by pywin32, you will get the given error (ImportError: DLL load failed while importing win32api).

You can try to extend the PATH variable in the shell before starting Python.

On Windows:

set PATH=c:\...\Lib\site-packages\pywin32_system32;%PATH%

On Unix like systems:

export PATH=c:\...\Lib\site-packages\pywin32_system32:$PATH

If that doesn't work, then the PATH maybe overridden within the Python program at runtime. Add the following line to your program just before pywin32 is used to verify its value:

import os
print(os.environ["PATH"])

As a last resort, you can extend the PATH variable before pywin32 is loaded:

Windows:

os.environ["PATH"] = r"c:\...\pywin32_system32;" + os.environ["PATH"]

Unix like:

os.environ["PATH"] = r"/.../pywin32_system32:" + os.environ["PATH"]
ocroquette
  • 2,315
  • 1
  • 17
  • 20
  • 2
    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. – pansila Nov 17 '20 at 03:11
  • 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. – ocroquette Nov 17 '20 at 07:09
3

According to pywin32 github you must run

pip install pywin32

and after that, you must run

python path\to\python\Scripts\pywin32_postinstall.py -install

taken from here. worked for me!

2

pypiwin32 is an outdated distribution. Uninstall it and install pywin32:

pip uninstall pypiwin32
pip install pywin32
phd
  • 57,284
  • 10
  • 68
  • 103
  • 1
    thank 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 "", line 1, in ImportError: DLL load failed while importing win32api: Impossibile trovare il modulo specificato. ` – Glxce Oct 29 '19 at 22:33
  • Try to reinstall it; after all your experiments with different packages the DLLs could be broken: `pip install --ignore-installed pywin32` – phd Oct 29 '19 at 23:40
  • 1
    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! – Glxce Oct 30 '19 at 14:05
  • 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. – vlad Oct 18 '20 at 02:56
1

For python 3.8.3, pywin32==225 worked for me, the existing pywin32==228 was uninstalled.

So try this --> pip install pywin32==225

Hope it solves your problem

0

In the referecne to this comment question

where is this Lib\site-packages\pywin32_system32 path?

Go to C directory, Users , your username , anaconda3 ,Lib, site-packages,pywin32_system32. you can find easily.

C:\Users\HP\anaconda3\Lib\site-packages\pywin32_system32

Ardent Coder
  • 3,309
  • 9
  • 18
  • 39
Aastha
  • 1
  • 1
0

Currently there are two copies of the pythoncom*.dll files in directories. Pycharm is using the copy in directory C:\Windows\System32:-

C:\Windows\System32 C:\Users\sharandi\AppData\Local\Programs\Python\Python38\Lib\site-packages\pywin32_system32

The files are: - pythoncom38.dll - 559 KB pywintypes38.dll - 138 KB

0

I downloaded and installed the latest Microsoft Visual C++ 2015-2019 package from (https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads). You may have to restart your machine after installation.

Then from conda command prompt activate your virtual environment and go to the Scripts folder of the virtual environment(>cd "path of venv"/Scripts)

Once you are in the Scripts folder run the following command "python pywin32_postinstall.py -install"

This will install the required dll's to the appropriate folders in the virtual environment.

And that's how I got it to work!

0

Jupyter notebook github has the issue mentioned in the question. There are multiple solution proposed.

What worked for me was this answer with additional first step:

  1. pip uninstall pywin32
  2. pip install pywin32
  3. python [environment path]/Scripts/pywin32_postinstall.py -install
Vikram
  • 88
  • 11