0

I am trying to get a screenshot in Python 3.8. When I search in google for the fastest way I found this link. Fastest way to take a screenshot with python on windows

There is some module needed to install in this code. I tired to install them with command below.

pip install pywin32

After that command I could import win23con to my project. But when I try to import win32gui and win32ui gave error in import lines. I shared error titles in below.

import win32con
import win32gui #No module named 'win32gui'
import win32ui #No module named 'win32ui'

Screenshot for error from PyCharm

After some research on google. I fount this answer in stackoverflow. https://stackoverflow.com/a/44065593/17257421

If I understand corretly, I have to use Phyton-32bit version to use pywin32 module. I swith my project to Phyton 3.8 32-bit. But still I get same error.

Where I try select Phyton Version for my Project

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
Arif Demir
  • 1
  • 1
  • 3
  • You do NOT have to use 32-bit Python. If you are running 64-bit Windows, then you almost always want 64-bit Python. Did you try to run this, or are you just blindly believing the IDE? – Tim Roberts Oct 27 '21 at 05:53
  • Although using *064bit* versions is recommended, it's **NOT** the solution to your problem. *032bit* should work as well. I notice you're creating a new *VEnv*. Does the base interpreter have *PyWin32* installed? Try using the system interpreter instead. Are the modules available from *Python* console? – CristiFati Oct 27 '21 at 15:50

2 Answers2

0

pywin32 works just fine under 64-bit Python, so no, you do not need to use a 32-bit one -- switch to 64-bit Python on a 64-bit system, unless you specifically need 32-bit one for some specific reason. That does not seem to be the case here, though.

Also, if you have import-errors, it may be because your library is incomplete or old:

  • check what version you have installed at the moment with pip list -- the currently latest version as of writing this is 302.
  • run "pip uninstall pywin32" to remove whatever you have installed
  • check that it didn't leave any leftover pywin32-related files under your Python libraries (use the snippet below to check the path, if you don't know where to look)
  • after clearing out any leftovers, try installing pywin32 again and see if it works correctly now

How to get the path to Python's libraries:

import win32con, os
print(os.path.dirname(win32con.__file__).rsplit("\\", 2)[0])

It'll spit out something akin to e.g.: 'C:\Users\werecatf\pywin.venv\lib\site-packages'

The actual path will be different for you, but look there for anything related to "win32" and move those out somewhere else or delete them.

WereCatf
  • 321
  • 2
  • 4
  • I switched project to Phyton 3.8 64 bit version. Then uninstall from terminal and deleted all folders related to win32. Then install again using "pip install pywin32" and checked verison using "pip list". Currently on my project "pywin32 302" is installed. But same error still. – Arif Demir Oct 27 '21 at 07:06
  • Does "from win32 import win32gui" work? – WereCatf Oct 27 '21 at 07:37
  • If the above doesn't work, then you definitely have 32-bit and 64-bit libraries all mixed up and you should uninstall both 32-bit and 64-bit Python, delete the entire site-packages folder and reinstall only the 64-bit Python and start again. – WereCatf Oct 27 '21 at 07:52
  • I tried "from win32 import win32gui" and error is "Cannot find reference 'win32gui' in 'imported module win32'" – Arif Demir Oct 27 '21 at 09:31
  • I deleted all version of Python from my computer. Then downloaded Phyton 3.8.0 64 bit verison. Then Created new project from PyCharm and write "pip install pywin32" to terminal. Still same error. Also tried "from win32 import win32gui" same error. – Arif Demir Oct 27 '21 at 09:33
  • Then you still have the same issue. I don't use PyCharm, so I don't know any specific details on how it works, but it sounds like either you have multiple versions of Python installed and PyCharm uses a different one than what you use on the terminal, or pip keeps installing the wrong version. You could also attempt to purge pip's cache with "pip cache purge" – WereCatf Oct 27 '21 at 09:40
  • I downloaded visual code and its working. – Arif Demir Oct 27 '21 at 11:23
0

if you use conda, let remove current pywin32 and reinstall with conda command

uninstall:pip uninstall pywin32

install with conda: conda install pywin32

maybe you will get error when import win32api solutions: go to conda env directory run post_install command: python Scripts/pywin32_postinstall.py -install

for venv: you can go to venv directory run command like conda python Scripts/pywin32_postinstall.py -install

the above command will copy pywin32 dll(s) file to windows system32 folder

Khang Pham
  • 24
  • 2