Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] Issue KeyError: DISPLAY #313

Open
cevinov opened this issue Jan 21, 2024 · 4 comments
Open

[QUESTION] Issue KeyError: DISPLAY #313

cevinov opened this issue Jan 21, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@cevinov
Copy link

cevinov commented Jan 21, 2024

Description

Issue I am facing
Hello everyone, I have a question when I try to run my code. I got this error msg: KeyError: 'DISPLAY'. Please check in the below for the traceback.

Thanks.

Traceback to the issue

Traceback (most recent call last):
  File "/config/workspace/codebase/func_whatsapp_notif.py", line 1, in <module>
    import pywhatkit
  File "/usr/local/lib/python3.11/dist-packages/pywhatkit/__init__.py", line 16, in <module>
    from pywhatkit.whats import (
  File "/usr/local/lib/python3.11/dist-packages/pywhatkit/whats.py", line 7, in <module>
    import pyautogui as pg
  File "/usr/local/lib/python3.11/dist-packages/pyautogui/__init__.py", line 246, in <module>
    import mouseinfo
  File "/usr/local/lib/python3.11/dist-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
                       ~~~~~~~~~~^^^^^^^^^^^
  File "<frozen os>", line 679, in __getitem__
KeyError: 'DISPLAY'

Related part of my code

import pywhatkit

# Send a WhatsApp Message to a Contact at 1:30 PM
pywhatkit.sendwhatmsg("+xxxxxxx", "Hi", 13, 30)

Version Information

  • OS: Ubuntu 22.04.3 LTS
  • Python: 3.11.6
  • pywhatkit 5.4
@cevinov cevinov added the bug Something isn't working label Jan 21, 2024
@cevinov cevinov changed the title [QUESTION] Error when importing package [QUESTION] Issue KeyError: DISPLAY Jan 21, 2024
@MatteoSid
Copy link

I have the same problem and I seen that is a common issue in the web.

@rizz-sd
Copy link

rizz-sd commented Feb 14, 2024

I think this environment variable issue, can be easily fixed by just adding

import os

os.environ['DISPLAY'] = ':0

to the code, imo

@Nov05
Copy link

Nov05 commented Feb 19, 2024

I think this environment variable issue, can be easily fixed by just adding

import os

os.environ['DISPLAY'] = ':0

to the code, imo

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
[/usr/local/lib/python3.10/dist-packages/Xlib/support/unix_connect.py](https://localhost:8080/#) in get_socket(dname, host, dno)
     75             s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
---> 76             s.connect('/tmp/.X11-unix/X%d' % dno)
     77     except OSError as val:

FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

DisplayConnectionError                    Traceback (most recent call last)
10 frames
[/usr/local/lib/python3.10/dist-packages/Xlib/support/unix_connect.py](https://localhost:8080/#) in get_socket(dname, host, dno)
     76             s.connect('/tmp/.X11-unix/X%d' % dno)
     77     except OSError as val:
---> 78         raise error.DisplayConnectionError(dname, str(val))
     79 
     80     # Make sure that the connection isn't inherited in child processes

DisplayConnectionError: Can't connect to display ":0": [Errno 2] No such file or directory

👉 I was using:

  • Ubuntu 22.04.3 LTS (Jammy Jellyfish)
  • Python 3.10.12
  • pywhatkit-5.4.1

👉 check the full error logs

@Nov05
Copy link

Nov05 commented Feb 19, 2024

👉 I am using:

  • Ubuntu 22.04.3 LTS (Jammy Jellyfish, in Google Colab)
  • Python 3.10.12
  • pywhatkit-5.4.1

I am able to fix the error messages by using virtual display. However, my WhatsApp didn't receive any messages. I believe it is related to this issue sendwhatmsg_instantly() sometimes not sending message #308. If so, I don't know how to make this line of code web.open(f"https://web.whatsapp.com/send?phone={phone_no}&text={quote(message)}"), which launches a web browser, work in Google Colab. Hope anyone here can help. Thanks!

%%capture
!git clone https://github.com/Ankit404butfound/PyWhatKit
!pip install git+file:///content/PyWhatKit ## Successfully installed pywhatkit-5.4.1
!apt-get install xvfb
!pip install pyvirtualdisplay
!touch /root/.Xauthority
## Ctrl+M. restart the session
import os
os.environ['DISPLAY']='localhost:0'
print(os.environ['DISPLAY'])
import matplotlib.pyplot as plt
%matplotlib inline
from pyvirtualdisplay import Display
display = Display(visible=0, size=(1400, 900))
display.start()
is_ipython = 'inline' in plt.get_backend()
if is_ipython:
    from IPython import display
plt.ion();
## Sending to WhatsApp
from google.colab import userdata
my_phone_number = userdata.get('my_phone_number')
import pywhatkit
message = "Training is finished."
pywhatkit.sendwhatmsg_instantly(my_phone_number, message)
print("WhatsApp message sent!")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants