Cannot connect to X server GOOGLE COLAB

I had the same problem in Colab for a simple OpenCV program to track a tennis ball in a tennis match video and had to comment out these lines, as mentioned by @anand:

#cv2.namedWindow("motion")
#cv2.imshow("motion", img)# モーション画像を表示 

#cv2.destroyAllWindows()

Note: the above lines were not clustered but spread apart.


An X server is a program in the X Window System that runs on local machines (i.e. the computers used directly by users) and handles all access to the graphics cards, display screens and input devices (typically a keyboard and mouse) on those computers.

With that said Colab runs as a terminal instance in the server, if you are using GPU runtime, then the problem is not with X server accessing your Graphics card, neither with Input devices, generally this occurs when you try to parse some data that should be displayed as separate window on your desktop, commands like cv2.imshow(), there can be other similar functions that can cause this problem, if you have to use graphical ouput you might want to look into %matplotlib notebook and displaying the data in the interactable matplot plots.

If this is not your issue, just post a link to your modified code and I might be able to help more.