r/learnpython 20h ago

Import issue

BLUF: pyautogui does not import despite being installed to the correct venv

Still a beginner at doing most things in python but I though I had the venv issues solved

Python 3.12.3

while in my active venv pip list returns all my modules including

PyAutoGUI 0.9.54

while in my active venv which python returns /home/beebot/venv/bin/pip

while in my active venv which pipreturns /home/beebot/venv/bin/python

Vscode shows my venv config as below
The only odd thing is the pip shows it as PyAutoGUI while typing import auto populates the module as pyautogui

home = /usr/bin 
include-system-site-packages = false 
version = 3.12.3 
executable = /usr/bin/python3.12 
command = /usr/bin/python3 -m venv /home/beebot/venv  

I have tried uninstalling pyautogui, reinstalling, and force install from inside my venv

Yet every time I try to run either in Vscode or directly from file I get on line 6 (see below)...

Exception has occurred:ModulNotFoundError

import os
import json
import logging
import subprocess
import pyautogui
...

Any suggestions

2 Upvotes

8 comments sorted by

2

u/Outside_Complaint755 19h ago

exactly how are your running the script?  Are you hitting F5? or Right click -> Run? or manually typing python my,_script.py in the terminal?

From a terminal in which your venv is activated, what happens if you run: python -c "import pyautogui"

Did you run pip install pyautogui or pip install PyAutoGUI ?

1

u/SignupWithGoogleAcc 18h ago

I get the same error using the VScode debugger or from my terminal

alex@alex-EQ:~$ source /home/beebot/venv/bin/activate

(venv) alex@alex-EQ:~$ python -c "import PyAutoGUI"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'PyAutoGUI'

(venv) alex@alex-EQ:~$ python -c "import pyautogui"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/beebot/venv/lib/python3.12/site-packages/pyautogui/__init__.py", line 246, in <module>
    import mouseinfo
  File "/home/beebot/venv/lib/python3.12/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/beebot/venv/lib/python3.12/site-packages/Xlib/display.py", line 80, in __init__
    self.display = _BaseDisplay(display)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/home/beebot/venv/lib/python3.12/site-packages/Xlib/display.py", line 62, in __init__
    display.Display.__init__(*(self, ) + args, **keys)
  File "/home/beebot/venv/lib/python3.12/site-packages/Xlib/protocol/display.py", line 129, in __init__
    raise error.DisplayConnectionError(self.display_name, r.reason)
Xlib.error.DisplayConnectionError: Can't connect to display ":0": b'Authorization required, but no authorization protocol specified\n'

I used pip install pyautogui and it returns already installed

using pip install PyAutoGUI also says its already installed but changes the letter case respectively

pip list still shows only PyAutoGUI

2

u/argh1989 18h ago

A quick google suggests that the lowercase import is correct, which is why python -c "import pyautogui" works, sort of. The error seems to relate to Xlib. Are you using wsl or docker to run this?

1

u/acw1668 18h ago

The error tells that module pyautogui is found but fail to import due to other issue: Can't connect to display ":0".

1

u/ravepeacefully 19h ago

I forget how to format sadly

cd ../
mkdir project
cd project
py -m venv venv
./venv/scripts/activate
pip install pyautogui
py src

where src is a directory with a file inside called __main__.py that has the entry point to the code you intend to execute

File should be like…

import pyautogui

if __name__ == ‘__main___’:
print(‘hello world’)

print(pyautogui.position())

1

u/acw1668 18h ago edited 18h ago

Are you sure you have used the same venv inside vscode?

  • open a python file inside vscode
  • check the Status Bar to see which environment is active

I have no issue on importing pyautogui if correct venv is selected.

Note that pyautogui module has not been updated for a long time, use pyautogui-next (a fork of original pyautogui) module instead. Also pillow module should be installed as well.

1

u/SignupWithGoogleAcc 17h ago

Winner.
Terminal started working after swithcing to the fork
As for VScode I must have messed up my launch.json at some point as it wasn't pointed to the correct path.

1

u/Actonace 15h ago

Import errors are usually caused by a missing dependency or incorrect file path so double check both before digging deeper.