Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import tkinter after installing Python 3 with pyenv

Tags:

python

I got problems with importing tkinter after installing Python version 3.4.2 with pyenv. My system Python is version 2.7.6. I am using Ubuntu 14.04. For the following sample script t.py:

import _tkinter

print ("Hello")

I get:

$ pyenv global system
$ python --version
Python 2.7.6
$ python t.py
Hello
$ pyenv global 3.4.2
$ python --version
Python 3.4.2
$ python t.py
Traceback (most recent call last):
  File "t.py", line 3, in <module>
    import _tkinter
ImportError: No module named '_tkinter'
    Traceback (most recent call last):
      File "t.py", line 3, in <module>
        import _tkinter
    ImportError: No module named '_tkinter'

Note that pyenv installed Python version 3.4.2 in ~/.pyenv/versions/3.4.2/.

like image 919
Håkon Hægland Avatar asked Oct 14 '14 09:10

Håkon Hægland


People also ask

How do I import Tkinter?

The simplest method to install Tkinter in a Windows environment is to download and install either ActivePython 3.8 or 3.7 from here. Alternatively, you can create and activate a Conda environment with Python 3.7 or greater that is integrated with the latest version of Tkinter.

Do I need to import Tkinter?

In order to work with a tkinter application, we have to install and import the tkinter library in our environment. Generally, we import the tkinter library in the environment by using from tkinter import * command. The significance of "import *" represents all the functions and built-in modules in the tkinter library.


1 Answers

This problem seems to be solved now using the approach described in this post:

  • First uninstall Python 3.4.2 : pyenv uninstall 3.4.2, then
  • Run sudo apt-get install tk-dev
  • And reinstall Python 3.4.2 : pyenv install 3.4.2
like image 131
Håkon Hægland Avatar answered Oct 17 '22 08:10

Håkon Hægland