Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python3 tkinter ubuntu trusty does not work under virtual environment

I installed python3-tk on an ubuntu trusty docker container.(apt-get install python3-tk)

> python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> import tkinter as tk
>>> 

So far so good. However, for different reasons, I need to run a loaded python3 virtualenv in same container. When I activate the virtual environment:

(env_py34)root@8a7953c24d4f:/home# python
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter as tk
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 39, in <module>
    import _tkinter
ImportError: No module named '_tkinter'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/tkinter/__init__.py", line 41, in <module>
    raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package
>>> 

Versions of python are identical. How come the virtual environment did not inherit python3-tk from the python3 installation? How can I install python3-tk inside the virtual environment?

like image 701
user1256124 Avatar asked Jan 20 '16 02:01

user1256124


3 Answers

So from the error message, python3-tk package is missed.

raise ImportError(str(msg) + ', please install the python3-tk package') ImportError: No module named '_tkinter', please install the python3-tk package

Did you try to install it? for example, in ubuntu

sudo apt-get install python3-tk
like image 176
BMW Avatar answered Nov 19 '22 04:11

BMW


I had the same problem. The answer provided by user BMW did not work for me. There is no need to recreate the virtualenv directory, like user1256124 suggests, although that is a perfectly workable solution as well.

Just use the bindings specific to the version of python that you need. For python3.6, this command is:

sudo apt-get install python3.6-tk

This allows an already set up virtualenv to find the right libraries.

like image 6
JG_Maine Avatar answered Nov 19 '22 02:11

JG_Maine


I figured it out. What was happening is that once python3 virtualenv was created there was no more inheriting to be done from the python3 apt-get installation. Once I recreated the image from the updated Dockerfile in which it was specified to apt-get install python3-tk BEFORE creating the python3 virtualenv, then everything worked. Not sure why this is the case though.

like image 3
user1256124 Avatar answered Nov 19 '22 02:11

user1256124