Pycharm: no display name and no $DISPLAY environment variable

I've been working with PyCharm for a couple of weeks now.  My development machine is a PC running Windows 10, and the target is a Raspberry Pi 3B+ running Raspberry Pi OS.  I have the remote settings configured so I can hit "run" on the Windows machine and it will update the code on the Pi, and run the first part of the code - (ssh://pi@192.168.1.3:22/usr/bin/python3 -u -m main), printing the text console output from the Pi on the PC, but as soon as I try to do anything with tkinter in the code, it tells me "no display name and no $DISPLAY environment variable".  I've tried various suggestions found using Google, but nothing seems to be working.  If I go to the HDMI/keyboard/mouse connected to the Pi and run "python3 main" in the target directory, the tkinter windows and buttons work fine.  Can you help?



Traceback (most recent call last):
File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/pi/projects/dt_test_fixture/main.py", line 20, in <module>
style = ttk.Style()
File "/usr/lib/python3.7/tkinter/ttk.py", line 368, in __init__
master = setup_master(master)
File "/usr/lib/python3.7/tkinter/ttk.py", line 354, in setup_master
master = tkinter._default_root or tkinter.Tk()
File "/usr/lib/python3.7/tkinter/__init__.py", line 2023, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

Process finished with exit code 1
1
3 comments

An old post but I will leave it here in case someone is looking for that.
 

First you need to install an “X Server” for Windows. I used Xming and it works just fine:
Xming X Server for Windows download | SourceForge.net

Next, go to the folder where Xming is installed and find a file called X0.hosts;
add your's RPi address there. It is kind of “firewall” - only IP addresses listed there are allowed to connect to your local Xming.;

Make sure your Windows Firewall is not blocking it - adjust if necessary;

Next, add this to your python code:
import os
os.environ['DISPLAY'] = ‘x.x.x.x:0’
where ‘x.x.x.x’ - is your IP. Not the IP of your RPi but the IP of your Windows machine. 
It tells the script location of the X server (which is Xming on your local machine).

Enjoy! :)

 

0

I have come across the same problem as well.

Similar set-up: a Windows 11 development machine that deploys some OpenGL app remotely to a Raspberry Pi 5 running RPi OS.

The fix I have found to the DISPLAY environment variable not being set correctly was to add a new environment variable to my run configurations:

DISPLAY=:0

It functions similarly to forcing the environment variable with 

import os
os.environ['DISPLAY'] = ‘x.x.x.x:0’

as the comment above mentions.

If this simple solution doesn't work, making sure you have a proper X-server set up as well as working out issues with X11 forwarding should be the next logical step.

I hope this can help someone!

0

I get this, only and only, when I run remotely from my professional pycharm, and not on the deployment server itself directly.  

I have tried almost anything that comes to mind and seems reasonable (including the above suggestions). Nothing has worked. 

This is very frustrating. As much as I don't like it, I might need to switch to VS.

/home/XYZ/vemprl/lib/python3.10/site-packages/glfw/__init__.py:914: GLFWError: (65544) b'X11: The DISPLAY environment variable is missing'
 warnings.warn(message, GLFWError)
/home/XYZ/vemprl/lib/python3.10/site-packages/glfw/__init__.py:914: GLFWError: (65537) b'The GLFW library is not initialized'
 warnings.warn(message, GLFWError)
Traceback (most recent call last):
 File "/home/XYZ/vemprl/code/PPO_MPRL_Planner.py", line 249, in <module>
   train(i, args)
 File "/home/XYZ/vemprl/code/PPO_MPRL_Planner.py", line 204, in train
   frame = env.render()
 File "/home/XVZ/vemprl/lib/python3.10/site-packages/gymnasium/core.py", line 471, in render
   return self.env.render()
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/wrappers/order_enforcing.py", line 70, in render
   return self.env.render(*args, **kwargs)
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/wrappers/env_checker.py", line 65, in render
   return env_render_passive_checker(self.env, *args, **kwargs)
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/utils/passive_env_checker.py", line 362, in env_render_passive_checker
   result = env.render()
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_env.py", line 409, in render
   return self.mujoco_renderer.render(
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 646, in render
   viewer = self._get_viewer(render_mode=render_mode)
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 686, in _get_viewer
   self.viewer = OffScreenViewer(self.model, self.data)
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 144, in __init__
   super().__init__(model, data, width, height)
 File "/home/XYZ/vemprl/lib/python3.10/site-packages/gymnasium/envs/mujoco/mujoco_rendering.py", line 61, in __init__
   self.con = mujoco.MjrContext(self.model, mujoco.mjtFontScale.mjFONTSCALE_150)
mujoco.FatalError: an OpenGL platform library has not been loaded into this process, this most likely means that a valid OpenGL context has not been created before mjr_makeContext was called
 

0

Please sign in to leave a comment.