1

So I am learning how to use tkinter and I am making a simple app, the problem happens when I try to open the application from the vscode terminal:

(base) leonardo@rosen:~/Documents/projects/simple_calculator$ ./calculator.py 
Traceback (most recent call last):
  File "./calculator.py", line 5, in <module>
    root = tk.Tk()
  File "/home/leonardo/anaconda3/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

Now the strange thing, is that I am able to run the application from the regular terminal, but not from the integrated terminal in vscode. Also, I have the code runner extension and if "run in the terminal" is set to false, it will also open the application. But the same error above will show up if it is set to true.

Lastly, running which python3 from both the integrated terminal and the regular terminal results in the same: /home/leonardo/anaconda3/bin/python3

I think it has something to do with how vscode deals with anaconda, since just opening the terminal results in the following (it automatically runs these commands to open a virtual environment):

leonardo@rosen:~/Documents/projects/simple_calculator$ source /home/leonardo/anaconda3/bin/activate
(base) leonardo@rosen:~/Documents/projects/simple_calculator$ conda activate base
(base) leonardo@rosen:~/Documents/projects/simple_calculator$ 

but the error occurs even if I deactivate the (base) environment. Is there a setting I should change? is it fine that it opens in (base) environment automatically?

leonardo
  • 125
  • 10
  • 1
    As an aside, why are you seemingly using the base environment for this? – AMC Jan 08 '20 at 16:56
  • @AMC vscode is doing that automatically, I actually don't know why. I am not actually using it though, since the integrated terminal is not doing what I want. I'm using the regular terminal and in there I am not using the base environment. – leonardo Jan 08 '20 at 16:59
  • 1
    What do you mean by _regular terminal_? You’re writing code in vscode, then switching to the terminal app and running it there? – AMC Jan 08 '20 at 17:01
  • that's correct. Sorry if that wasn't clear enough. I am using ubuntu 18.04 which comes with gnome-terminal and I am switching to it. It works as intended when I use that instead of the vscode integrated terminal. – leonardo Jan 08 '20 at 17:06
  • 1
    Possibly relevant question: https://stackoverflow.com/q/37604289/11301900 – AMC Jan 08 '20 at 17:49
  • 1
    As an aside, are you not using Conda environments at all? – AMC Jan 08 '20 at 17:50
  • What do you mean by that? For this little project I'm doing I'm not using a virtual environment, should I be using one even if I'm just playing around to get to know tkinter? I'm still learning the workflow and best practices. – leonardo Jan 08 '20 at 19:41
  • 1
    Personally I think it's worth using a virtual environment, just to be safe. It takes less than 2 minutes to set up, too, so it's not like there are any real downsides. – AMC Jan 08 '20 at 20:01
  • yeah I have been doing it as well and it's fairly easy. I guess I didn't know if it was good to do it everytime. Also, do you just delete your venv when you're done? because otherwise you might have a lot of repeated data on your pc, right? and I know you can recreate them with the requirements. – leonardo Jan 08 '20 at 20:39
  • 1
    Are you using _venv_ to refer to any virtual environment? Venv is also the name of a tool, so I want to make sure everything is clear. _Also, do you just delete your venv when you're done?_ It depends. I have a main environment which I use for any had-hoc stuff. I just delete an environment when I no longer need it. _because otherwise you might have a lot of repeated data on your pc, right?_ Unlikely, Conda will not download packages you already have again. Realistically, I can't see this ever being an issue. – AMC Jan 08 '20 at 20:45
  • 1
    As the error said, you need to define `DISPLAY` environment variable in the integrated terminal. Check the value of it in the "regular terminal". – acw1668 Jan 09 '20 at 00:14
  • @AMC yes I am using the tool venv, but as you said I was referring to the virtual environment. I has understood is somewhat standard to call the virtual environment venv in the project folder, though I know you can name it whatever you want. As for deleting virtual environments. I thought when you create one it has no packages and as you need them you basically install them again but in the new environment? at least that was my understanding of it, which is why I thought you would end up with repeated packages in multiple locations if you keep using them in different virtual environments. – leonardo Jan 09 '20 at 09:12
  • Ok I believe I fixed it, although it in an odd way. I went to the vscode settings>freatures>terminal. And there I found a setting called "Explorer Kind" which customizes what kind of terminal to launch. I changed it from integrated to external and I was able to run the application from inside vscode. Weirdly enough, I have changed it a couple of times between integrated and external and it is working both in the conda base virtual environment (which wasn't working before) and another one I just created. As @acw1668 said, I am pretty sure the problem was the `DISPLAY` environment variable. – leonardo Jan 09 '20 at 09:44
  • Before I made the change, when running `echo $DISPLAY` from gnome-terminal I got `:0` as a response. However, when I ran it inside vscode I just got an empty line. That being said, I tried to run `$DISPLAY=:0` but the command didn't work. I think it didn't like the `:0` in there, so I couldn't fix it that way. I probably did it wrong though, but once I did what I mentioned above, I can run the application from inside vscode and `echo $DISPLAY` returns `:0`. – leonardo Jan 09 '20 at 09:50
  • 1
    Use `export DISPLAY=:0` to set `DISPLAY` environment. – acw1668 Jan 09 '20 at 09:58
  • @acw1668 that makes sense, it got fixed the other way so I didn't mess more with that step. But thanks for teaching me that, I'm sure I'll need it soon! How can I mark this as resolved? I don't see an option for it. – leonardo Jan 09 '20 at 10:09
  • 1
    @leonardo _yes I am using the tool venv_ I’m confused, the result of `which python3`shows that you’re using Anaconda. – AMC Jan 09 '20 at 14:46
  • Sorry I realize it's confusing. I was just playing around so I wasn't using a virtual environment for this particular project and so whenever I used `which python3` it'd point to anaconda. I was using venv in general. But I did some more reading if I should use venv or anaconda. Initially I liked that venv would make the virtual environment right at the project directory. Conda can do something similar but I opted to just use conda environments in the conda directory and a bash script in the project folder to activate the conda environment. – leonardo Jan 10 '20 at 17:14
  • @leonardo What do you mean by _right at the project directory_? – AMC Jan 11 '20 at 00:38
  • Usually you can go to your project directory and use venv to create your virtual environment right there. At least that's how I was using it and tutorials I've seen use it. With conda, they're all located in the same place (unless you use `conda create --prefix `, I believe, but I chose not to do that). – leonardo Jan 11 '20 at 15:15

0 Answers0