BaronMartin
BaronMartin

Reputation: 61

ModuleNotFoundError: No module named 'win32api'

This is the error received:

    Traceback (most recent call last):
  File "C:/Users/Joe Martin/AppData/Local/Programs/Python/Python37/test.py", line 12, in <module>
    import win32com.client
  File "C:\Users\Joe Martin\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os
ModuleNotFoundError: No module named 'win32api'

This error occurs when trying to import the win32com.client module.

Solutions Tried:

I cannot find any other solution for how to fix this issue.

Upvotes: 2

Views: 18524

Answers (4)

Robert Rouse
Robert Rouse

Reputation: 1

I fixed this problem by deleting the Python Environment and using the Global Default Environment. The correct Environment will have all the modules that you code is importing listed in the Python Environment window. enter image description here

Upvotes: 0

David I
David I

Reputation: 931

@JohnJairo Your solution worked for me, too.

from win32 import win32api

Upvotes: 1

I solved with:

from win32 import win32api

print("Width =", win32api.GetSystemMetrics(0))
print("Height =", win32api.GetSystemMetrics(1))

None of the others possibilities worked for me. Python 3.9.6 (on Windows) IDLE 3.9.6 tk version 8.6.9

Upvotes: 0

Drake Wu
Drake Wu

Reputation: 7170

This is usually because no PythonPath is appended after the package is installed. Check the file--pywin32.pth under the folder--\\PythonVersion\\Lib\\site-packages\\.

The content in the file is like below:

# .pth file for the PyWin32 extensions
win32
win32\lib
Pythonwin
# Entries needed for a "portable" installations, where the post_install script
# isn't run, which would normally copy the pywin32 core DLL files to either
# the top of the python directory.
# We just stick the source of these DLLs directly on the PATH.
import os;os.environ["PATH"]+=(';'+os.path.join(sitedir,"pywin32_system32"))

Or create a PYTHONPATH environment variables, and append the win32 and win32/lib path into it.

You could also add these two paths to Python in project temporarily

import sys
sys.path.append('\\PythonVersion\\lib\\site-packages\\win32')
sys.path.append('\\PythonVersion\\lib\\site-packages\\win32\\lib')

The addition of paths is only valid for the time being.

Upvotes: 5

Related Questions