Hello guys how are you ? i try to run a main.py whith sudo python3 main.py but i get this : Couldn't connect X server

can you help me please

New contributor
Noura Ghana is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Does running it without sudo work? – Eduardo Trápani Feb 15 at 1:54
  • 3
    Please edit your question to include relevant context - what OS/release are you running this on? what graphical environment? what does main.py do? – steeldriver Feb 15 at 1:56
  • 2
    Whatever your main.py does, looks like it needs to create a window, and it's depending on an X-based toolkit. Are you using an X session or a Wayland session? – user535733 Feb 15 at 1:57
  • 1
    @NouraGhana You should definitely know what your main.py contains or where it comes from. Lots of programs contain a file called main.py… Please edit your question to clarify. Also, why do you need sudo? A bad program won’t magically start to work by adding sudo, on the contrary, it might be able to make things worse… – Melebius Feb 15 at 8:46
  • 1
    If it’s your own program, please share your code, optimally a MCVE showing your problem. And sudo is designed for administrative tasks, don’t misuse it for “troubleshooting”. – Melebius Feb 15 at 11:14

Run export DISPLAY=:0.0 and then run your python code.

If that does not work, you can try the solution here -- Cannot connect to X Server when running app with sudo

Not the answer you're looking for? Browse other questions tagged or ask your own question.