After upgrading to the latest version of Python, the 3.10.0, one of my project starting that uses the win32api module starting not to work. When I run it, I received an error message saying “ModuleNotFoundError: No module named ‘win32api'”. It takes me a couple of hours to figure out what is happening and fortunately, I was able to find a solution.
Traceback (most recent call last):
File "C:\myscript.py", line 1, in <module>
from win32api import GetFileVersionInfo, LOWORD, HIWORD
ModuleNotFoundError: No module named 'win32api'
The problem lies on the missing DLL library of win32api, specifically the pythoncom310.dll and pywintypes310.dll. The new version of Python couldn’t interpret the path directory of these file and you have to manually change the path if you’re using a Windows computer.
We will guide you on how to fix it from scratch.
How to Fix win32api No module Error
First, you have to execute the script inside the Scripts directory, the pywin32_postinstall.py. Let’s say your Python directory is C:\python3, just follow the code below.
cd C:\python3
python Scripts/pywin32_postinstall.py -install
After that, the installation will drop the DLL files under the C:\Windows\System32
. You need to move those two files ( pythoncom310.dll and pywintypes310.dll) to C:\python3\Lib\site-packages\win32
directory.
After that, you need to edit the python310._pth that you can find inside the Python installation folder. Then make the following changes:
Lib/site-packages
Lib/site-packages/win32
Lib/site-packages/win32/lib
Lib/site-packages/pythonwin
python310.zip
.
# Uncomment to run site.main() automatically
#import site
Save and try running your code again.
Troubleshoot
If you still get an error saying “ImportError: DLL load failed while importing win32api: The specified module could not be found.”, make sure you have copied the two dll files to Lib\site-packages\win32
directory.