Tcl Error: No display name or display environment variable – How to fix it?

“Python is a well-known high-level programming language that is used a lot. The Python script is a file with code written in Python inside it. People who use Python write their scripts in files called “.py.” These files can also be called “.pyw” when they are being run on a computer that is called “Windows.” We need to download and install a Python interpreter to run a Python script. This needs to be done first. But sometimes we get an issue TclError: no display name and no $DISPLAY environment variable

Tcl Error: No display name or display environment variable - How to fix it?

We can fix tclerror: no display name and no display environment variable issue with simple steps by just adding some code. we can create a virtual display or setting the DISPLAY environment variable

Some people are getting this while saving plots with python and matplotlib.pyplot is running over ssh. The error show doing this gets _tkinter.TclError: couldn’t connect to display “:0.0” 

there are many issues with the error; if you are a coder, you are familiar with these types of problem while running a code. There are many error codes with Tclerror; the error caused during running a python script if you missed some time setting things.

What is Tcl Error No display name or display environment variable, and what does it mean?

This error means that either the Tcl script or the Tcl interpreter cannot find a display name or an environment for it to run in. You can try running the script with the -display name or -environment options to fix this. You can also try running the script in a different directory or with other Tcl options.

If you are getting an error when you try to run a Tcl script, there is probably a problem with the hand. Sometimes you can fix this by correcting the mistake, but other times it requires that you fix the script itself.

Have a look at here:- How do I get to tmp folder in Linux terminal? Quick Shortcut

TCL error is a message that you may see when using the Tcl interpreter. This message usually indicates that there is something wrong with the code that you are trying to run.

What is TclError?

The Tcl programming language has several errors when you try to run a Tcl program. Some common mistakes include the following:

  • An error in the Tcl interpreter
  • A syntax error in your Tcl code
  • A runtime error when you try to run your Tcl code
  • An error when you try to use a Tcl function or variable

If you get an error when you try to run your Tcl code, there are a few things you can do to try to find the source of the problem. One way to find the head of the problem is to look at the error message displayed when you run your Tcl program. Another way to find the source of the problem is to look at the Tcl code that you are trying to run. If the Tcl code that you are trying to run is not related to the error that you are getting, then you may need to look at the Tcl interpreter

Example of TclError

someone tries to run this script on the server

import matplotlib.pyplot as plt
import numpy as np

x = np.random.randn(60)
y = np.random.randn(60)

plt.scatter(x, y, s=20)

out_png = 'path/to/store/out_file.png'
plt.savefig(out_png, dpi=150)

after running the script, he gets an error; he try to use the command python example.py in this server which has matplotlib 1.5.1 installed, it fails with the error:

Traceback (most recent call last):
  File "example.py", line 7, in <module>
    plt.scatter(x, y, s=20)
  File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 3241, in scatter
    ax = gca()
  File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 928, in gca
    return gcf().gca(**kwargs)
  File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 578, in gcf
    return figure()
  File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/pyplot.py", line 527, in figure
**kwargs)
  File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 84, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/home/USER/.virtualenvs/nnet/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 92, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

this is an error that you might make while running the python script. However, we can fix the issue by adding some code before running the hand or by creating the virtual display.

How do you fix Tcl Error No display name or display environment variable?

Some experts provide more than one method so i will try to collect all and put them here. You can try one by one and see which way fixes you issue.

Steps to Fix Tcl Error No display name or display environment variable?

  • Add code before Running The Script
### CREATE VIRTUAL DISPLAY ###
!apt-get install -y xvfb # Install X Virtual Frame Buffer
import os
os.system('Xvfb :1 -screen 0 1600x1200x16  &')    # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
os.environ['DISPLAY']=':1.0'    # tell X clients to use our virtual DISPLAY :1.0.
%matplotlib inline
### INSTALL GHOSTSCRIPT (Required to display NLTK trees) ###
!apt install ghostscript python3-tk

this will create a virtual display and can fix the issue to run the python script

Method #2

sometimes this problem occur because Matplotlib chooses Xwindows backend by default.  You need to set matplotlib not to use the Xwindows backend.

Try again by adding the following code to the beginning of your script (before importing plot):

import matplotlib matplotlib.use('Agg')


Or add to .config/matplotlib/matplotlibrc line backend: Agg to use a non-interactive backend.

echo "backend: Agg" &gt; ~/.config/matplotlib/matplotlibrc

Or when connecting to server using ssh  X remoteMachine command to use Xwindows.

Also, you may try to export the display: <code>export DISPLAY=mymachine.com:0.0,

Method #3

Someone also fixed this. he used this at the beginning of the needed script. So it runs smoothly in different environments.

import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
    print('no display found. Using non-interactive Agg backend')
    mpl.use('Agg')
import matplotlib.pyplot as plt

Method #4

you can also solve it by adding these two lines at the VERY beginning of your .py script.

import matplotlib
matplotlib.use('Agg')

PS: If these two lines are not placed at the very beginning of the source code, the problem will persist.

Method #5

Another solution is to install Xvfb and export your display to it. i.e.:

disp=:8
screen=0
geom=640x480x24
exec Xvfb $disp -screen $screen $geom 2>/tmp/Xvfb.log &

Then

$ export DISPLAY=:8

$ ./example.py

this method that I provided above will definitely fix the issue Tcl Error: No display name or display environment variable or if you have a problem on the above methods, comment down below

What are the possible causes of Tcl Error No display name or display environment variable?

There are a few things that could cause a Tcl error. The most common causes are:

  1. missing required library or executable
  2. incorrect command line or path
  3. problem with your Tcl installation
  4. There is not enough memory available to start Tcl.
  5. The Tcl library files are not in the correct place.
  6. The Tcl library files are damaged.
  7. The Tcl library files are not installed correctly.
  8. The Tcl library files are not updated correctly.

So the above-listed causes might be one of the issues of your tclerror; simply check all the reasons and try to fix them one by one.

Conclusion

I hope you find the solution to Tcl Error No display name or display environment variable. If you have any issues with fixing it, you can comment below; we will look at it and try to solve the problem. you may simply push the above method and I’m sure you will get rid of this problem

Source

https://stackoverflow.com/questions/37604289/tkinter-tclerror-no-display-name-and-no-display-environment-variable