Python 'No module named win32gui' after installing pywin32
Asked Answered
N

3

4

Running python 3.6 on windows 8.

ModuleNotFoundError: No module named 'win32gui'

I have tried multiple installations of pywin32 but none have worked so far. https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/ goes through installation without problems but the issue persuades. Same with pip installing: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pywin32

In my Python root there is now multiple pywin32/win32 variants in the site-packages folder. pywin32.pth doesn't contain a win32gui but only the following:

# .pth file for the PyWin32 extensions win32 win32\lib Pythonwin

I could as a last resort switch to another GUI toolkit but as I'm using some opensource code that would mean I'd have to rewrite lots of code which is why it is a last resort.

Update: changing the import line from:

import win32gui, win32ui, win32con, win32api

To:

from win32 import win32gui, win32ui, win32con, win32api

Pushed the error to:

ImportError: cannot import name 'win32ui'
Nearsighted answered 19/5, 2017 at 6:54 Comment(1)
Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Compare stackoverflow.com/help/self-answerDarrelldarrelle
H
4

This looks very much like a 32-bit/64-bit issue. If you are running 64-bit Python and you have 32-bit PythonWin you will see this sort of thing. Both win32gui and win32ui are .pyd files (DLLs) and they should live in Lib\site-packages\win32 and Lib\site-packages\pythonwin respectively.

If you can see them there but the import is failing then it is likely they are the wrong bitness. A 64-bit executable cannot load a 32-bit DLL and vice versa, and if you try, in most cases the error message will tell you that the DLL you are trying to load isn't there. Even when you can see that it is.

Edit following exchange of comments with OP:

You will also get this sort of error with PythonWin if you put multiple imports in a single line. Follow PEP-8 and do one import to a line.

Houseclean answered 19/5, 2017 at 8:55 Comment(5)
I checked my Python version and via 'platform.architecture()' it says im running 32 bit. '3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]'. Both those .pyd files are in their respective folders.Nearsighted
When I do this from win32 import win32gui, win32ui, win32con, win32api get the same error as you report. That is because win32ui isn't in win32.Houseclean
You mean this could be an issue with the the module and with what I'm doing?Nearsighted
Have you tried putting the import statements on different lines?Houseclean
That seems to have done the trick! As always solution is pretty simple :) Do I accept your answer in general as the solution is dividing the imports?Nearsighted
A
4
from win32.win32gui import FindWindow, GetWindowRect, MoveWindow

I guess the standard model name is win32, so all the tutorial on web is outdated. For whatever said before v300, add"win32." before the model name to give a parent modelname which is "win32"

Ambiguity answered 27/11, 2020 at 7:31 Comment(0)
R
1

This fixed the problem for me. (Compare solution provided by question author inside the question; now deleted.)

from win32 import win32gui
Rhine answered 17/1, 2023 at 8:25 Comment(1)
Please turn this into an answer according to How to Answer, based on the solution provided by the author of the quesiton. That solution part I will remove, because it is not part of the question - or should not be. Please use all of the solution in your answer and mention that you only needed the first part if you like.Darrelldarrelle

© 2022 - 2024 — McMap. All rights reserved.