0

I'm getting a "no $DISPLAY environment variable" with Python on macOS. That's MacPorts python 3.7, not the native python. I want to use MacPorts python with virtual environments so I can keep my projects clean. I also have to use MacPorts not Homebrew.

I've unsuccessfully tried setting the $DISPLAY value to :0.0 and the following link doesn't help because I'm not using matplotlib and I'm running on a machine with a display Issue with tkinter, python and seaborn: _tkinter.TclError: no display name and no $DISPLAY environment variable

I understand from here [https://www.python.org/download/mac/tcltk/#built-in-8-6-8] that using X11 is no longer the recommended path, rather using Aqua Cocoa Tk is.

The question: What's the trick to getting this environment variable set correctly?

My stack is:

macOS Mojave

Macports with the following relevant ports installed:

  • py37-tkinter @3.7.4 python/py-tkinter
  • py37-virtualenv @16.7.2 python/py-virtualenv
  • python37 @3.7.4 lang/python37
  • tcl @8.6.9 lang/tcl
  • tk @8.6.9 x11/tk
  • virtualenv_select @0.1 python/virtualenv_select

when I run

import tkinter as tk
tk._test()

I get

_tkinter.TclError: no display name and no $DISPLAY environment variable
Brooks
  • 2,082
  • 2
  • 18
  • 26

2 Answers2

1

Fairly recent versions of macOS don't have an Xserver by default, so you'll need to install one as well. XQuartz works enough as far as I'm aware (i.e., I'm using it).

The other option is to use a build of Tk that's using the Aqua back end instead of the X11 back end.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • ActiveState TCL should be that right? I installed it but Python is still giving me the same error. Do I need to rebuild Python or is there an easier way to point it at ActiveState? – Brooks Aug 09 '19 at 00:39
1

I had the same issue, down to the wire. Could not get tkinter to behave properly with any sort of graphical interfaces, wanted to be consistent in using MacPorts for my Python files, and I am not currently using matplotlib for anything.

Running macOS 10.15.5, I found that the problem seemed to be that my tk port and my xorg-server ports weren't communicating with one another politely. I had installed the former as a dependency; as a result, the former installation defaulted to the x11 variant (as is the case with OP's port).

I'm not sure about how to make the Aqua Cocoa version work, but reinstalling tk and specifying the quartz variant solved the problem for me:

sudo port install tk +quartz

If this works for you, your call to to tk._test() ought to stop returning your given TclError.

Hopefully.