Open odidev opened 3 years ago
You're on Linux right? Can you give me the distro and version? Is this a headless machine without a monitor?
(Note for my future self: I need to look into the DISPLAY environment variable and when it isn't set. This page has some more info: https://askubuntu.com/questions/432255/what-is-the-display-environment-variable )
The problem is that pyautogui
initializes the code needed for the display interaction at import time.
Even if you don't test the GUI interaction or even use any part of pyautogui
, when you import something from a module that also imports pyautogui
your headless CI will crash if you don't have some kind of virtual display available.
This is my drone CI pipeline which can also run with modules that import pyautogui
.
kind: pipeline
type: docker
name: tests
trigger:
branch:
- main
event:
- push
- pull_request
steps:
- name: Install dependencies and run tests
image: python:3.8-buster
commands:
- apt update
- apt install -y xvfb xserver-xephyr xauth
- touch ~/.Xauthority
- python -m pip install pytest-xvfb
# all the above is only needed for the CI an has nothing to do with the tests themselves
- python -m pip install -r requirements_dev.txt
- python -m pip install .
- python -m pytest
For Linux you can install xvfb
as virtual display and set it as the display to be used (that is what pytest-xvfb
does in my example pipeline).
As a side note if you import from modules that also import pyautogui
in you conftest.py
you need to wrap those imports inside of functions/classes, so the import happens at runtime and not at import time, because pytest-xvfb
isn't fully initialized at that time.
Hope this helps someone 😄
Thanks @s-weigand for your response.
As suggested, I installed xvfb
but still got the same DISPLAY error for tox
command, also it is giving the same error with MobaXterm.
While using pytest, I got 11 failures with different errors:
Test_using_pytest: pyautogui_pytest_result.txt Test_using_tox: pyautogui_tox_result.txt
@asweigart Is there any update for the above issue ?
@odidev The tox error is because you run your test script directly with python and not with pytest
and pytest-xvfb
(pytest-xvfb
set up all the env vars when fixtures get initialized).
The pytest
errors look mostly like usage errors to me:
elif secondArg is None and firstArg is not None:
> return Point(int(firstArg), int(position()[1]))
E TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
Where firstArg = [0, 0], secondArg = None
Hi @s-weigand After installation of pytest-xvfb ran the test script with pytest still getting the same above error.
@odidev As I said that error looks like a usage error to me, since you try to cast the list firstArg = [0, 0]
to an int
.
Judging from the docstring:
firstArg
andsecondArg
can be integers, a sequence of integers, or a string representing an image filename to find on the screen (and return the center coordinates of).
You need to do more checks than just checking if those values are None
, since the code will break for all cases but int
(or strings which can be cast to int). Thus your error is unrelated to pyautogui
I write a code and I can run it with vs code or by myself. I try to run the code as a service on my linux. And it got such error
7月 17 08:55:53 hp python[62308]: import pyautogui
7月 17 08:55:53 hp python[62308]: File "/home/mifen/.conda/envs/310/lib/python3.10/site-packages/pyautogui/__init__.py", line 246, in <module>
7月 17 08:55:53 hp python[62308]: import mouseinfo
7月 17 08:55:53 hp python[62308]: File "/home/mifen/.conda/envs/310/lib/python3.10/site-packages/mouseinfo/__init__.py", line 223, in <module>
7月 17 08:55:53 hp python[62308]: _display = Display(os.environ['DISPLAY'])
7月 17 08:55:53 hp python[62308]: File "/home/mifen/.conda/envs/310/lib/python3.10/os.py", line 680, in __getitem__
7月 17 08:55:54 hp python[62308]: raise KeyError(key) from None
7月 17 08:55:54 hp python[62308]: KeyError: 'DISPLAY'
just like @ s-weigand said, in system service, there is no display.
I have been working on installing and testing the pyautogui package locally on an x86_64 machine. I followed the docs and installed all the dependencies successfully, While testing it failed for the command tox -e py37, Please find below the error details:
It would be really helpful if you could share some pointers on how to resolve the above issue.
Environment detail: PyAutoGUI v0.9.52 Python 3.7.10 pip 20.1.1 Pytest 6.2.4