38

Trying to run python code with TKinter-based widgets from a virtualenv.

    user@computer:~/myproject$ env/bin/python Python
    2.7.3 (default, Sep 26 2012, 21:51:14)  [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
    >>> import Tkinter 
Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
        raise ImportError, str(msg) + ', please install the python-tk package' ImportError: No module named _tkinter, please install the python-tk package

What is the proper way to install TK to the virtualenv?

2
  • 2
    I don't know anything specifically about TKinter, but I've run into similar issues trying to get wxPython and pyGTK working inside a virtualenv. I was never able to find a satisfactory method for installing these into a virtualenv because they are heavily dependent on shared libraries. Do you need some specific version of Tkinter other than the one in your system site packages? Otherwise I would recommend just creating a virtualenv with the --system-site-packages flag, then locally installing whatever other specific modules you need.
    – ali_m
    Apr 8, 2013 at 18:24
  • @Jasper van den Bosch Since your edit was rejected (reviewers considered it was invalid, but it is very useful indeed), I have added it to my answer, feel free to improve it.
    – A. Rodas
    Apr 11, 2013 at 9:46

9 Answers 9

48
+50

Set the environment variable TCL_LIBRARY in your activate script. On Windows (Python 2.7 with Tcl 8.5), just add this line to Scripts\activate.bat:

set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5"

@Jasper van den Bosch's edit: On Ubuntu, the modification to the script activate is the following:

TK_LIBRARY=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib
TKPATH=/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/site-packages/PIL:/usr/lib 
TCL_LIBRARY=/usr/lib 
export TCL_LIBRARY TK_LIBRARY TKPATH

The reference of this can be found on this question on askubuntu

3
  • 5
    For ease of use I would suggest initializing virtualenv with --system-site-packages.
    – luka5z
    Aug 12, 2016 at 6:54
  • 5
    How would you do this in Python 3? Mar 20, 2018 at 19:02
  • new virtualenv with same python(no upgrades or changes - same python used to create previous virtualenv) version works for me Feb 21, 2019 at 7:33
22

I manage to integrate tkinter in python3 to virtualenv by symlink tkinter folder to virtualenv python3. I works for me. But I don't know if it's the right way.

  1. install tkinter
sudo apt-get install python3-tk
  1. go to your virtualenv's python lib folder
cd ~/.virtualenvs/cv/lib/python3.4/
  1. link the tkinter
ln -s /usr/lib/python3.4/tkinter tkinter

Hope this helps.

In later versions of python, this may result in a

ModuleNotFoundError: No module named '_tkinter'

In this case, ensure to also symlink
/usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so
as path/to/virtualenv/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so using

ln -s /usr/lib/python3.x/lib-dynload/_tkinter.cpython-36m-x86_64-linux-gnu.so _tkinter.cpython-36m-x86_64-linux-gnu.so

from within your virtualenv lib/python3.x/lib-dynload/ directory.

5
  • 3
    I don't see the point using a virtualenv if you're going to use sudo to install it. Jul 8, 2016 at 9:47
  • 2
    The sudo is to install the binary library that is Tk. The virtualenv just needs a symlink to know where to look.
    – Ari Gesher
    Aug 8, 2016 at 17:56
  • In addtion to the "tkinter" i had to symlink "ln -s /usr/lib/python3.5/lib-dynload/_tkinter.cpython-35m-x86_64-linux-gnu.so _tkinter.cpython-35m-x86_64-linux-gnu.so" on python-3.5 to make it work Mar 1, 2019 at 22:27
  • Under Ubuntu 18.04 with pyenv virtual environments, I had to place the symlinks in the installed base python rather than in the virtual version directories. E.g. cd ~/.pyenv/versions/3.7.3/lib/python3.7/ and then ln -s /usr/lib/python3.7/tkinter tkinter and cd ln -s /usr/lib/python3.7/lib-dynload/_tkinter.cpython-37m-x86_64-linux-gnu.so lib-dynload/_tkinter.cpython-37m-x86_64-linux-gnu.so Aug 31, 2019 at 9:00
  • It is broken again on Python 3.10 (on Linux Mint 21), and this time there is no directory with the word dynload in any part of the name. If I run the vm in a Terminal and run the source command, everything works, but if I run the program using Visual Studio using the vm's python executable, I see the same source command run but it doesn't work ("ModuleNotFoundError: No module named 'tkinter'"). So strange...
    – Poikilos
    Jul 22 at 23:59
10

I am using python2.7 with a virtualenv on a machine running linux mint. I received the exact same error as mentioned by the OP when running an application that required matplotlib in my virtualenv. "ImportError: No module named _tkinter, please install the python-tk package"

I ended up deleting and recreating my virtual environment using the suggestions in the above posts. Here are my steps:

  1. if your virtual environment is activated, then run this command to freeze the requirements to a file that you can access later: pip freeze > requirements.txt
  2. if your virtual environment is activated, then deactivate it using: deactivate
  3. delete your virtualenv folder.
  4. install python-tk using: sudo apt-get install python-tk
  5. recreate your virtualenv using: virtualenv <nameofyourenv> --system-site-packages
  6. next, activate your virtualenv: source <virtual environment folder>/bin/activate
  7. restore all your packages that you froze earlier from the requirements.txt file: pip install -r <path to requirements.txt file>

now, when I run the same code as before, it has no problem importing _tkinter. Hope this helps! Thanks to the everyone's suggestions above. It really helped me a lot.

6

This is really an update to the great answer from A. Rodas for use with Python 3.4 and Tcl 8.6 (I don't have enough reputation points to comment).

Set the environment variable TCL_LIBRARY in your activate script. On Windows (Python 3.4 with Tcl 8.6), just add this line to Scripts\activate.bat:

set "TCL_LIBRARY=C:\Python34\tcl\tcl8.6"

I came across this issue while using Windows 7, Python 3.4, and ggplot in a virtual environment.

1
  • This solution worked for me. Except that I had to find out where python is installed on my Windows machine.
    – Omar Tariq
    May 3, 2016 at 11:56
1

To get this working in powershell, you have to edit the activate.ps1 file instead of activate.bat. Just add the following to activate.ps1: $env:TCL_LIBRARY = "C:\Python27\tcl\tcl8.5"

1

clone the cpython project

git clone [email protected]:python/cpython.git

cd to the cpython directory. git checkout the desired version of your virtual env(for me it is 3.4), and build it with

./configure
make
make test
sudo make install

you will find an so file _tkinter.cpython-xxx.so in a subdir of the build/ directory, copy it to your venv's lib-dynload dir. (for me it is ~/tf1.1py3.4/lib/python3.4/lib-dynload/)

2
  • This also requires that you have the Tk libs installed (i.e. sudo apt install tk-dev), and you would want to make sure to checkout the version corresponding to the python version used in your venv (i.e. git checkout tags/v3.6.8 -b v3.6.8). Mar 27, 2019 at 8:32
  • Forgot to mention, my problem was that I had compiled Python myself, and I didn't have the Tk libs at the time. Installing those and recompiling/installing did the trick, as my venv was still referencing the same install location. Mar 27, 2019 at 9:31
0

Also an update from answer by A.Rodas - I have tried doing that in Windows 7 using Windows Powershell but wasn't able to get it done (I also do not have enough reputation points to comment

I realized even when I added the line set "TCL_LIBRARY=C:\Python27\tcl\tcl8.5" and the corresponding one for the tk library, to the activate.bat script, the tcl link was not getting updated, and what I needed to do was just go to the directory where it's looking for the tcl files, and copy the corresponding tcl and tk files to that directory. Now the file is in the default location so you don't need to update activate.bat everytime you create a new environment

1
  • I have the same exact issue, modifying the script does not work, the environment variable is not set using pew for virtualenv management. Trying the fix from @user2623839 does not work either, TCL_LIBRARY is not set. You don't happen to be using pew as well by any chance ?
    – Overdrivr
    Feb 3, 2016 at 14:24
0

For me the solution was copying the folder tcl from

C:\Users\{myUser}\Python3.5\tcl

to

C:\Users\{myUser}\{myVirtualEnv}

replace {myUser} and {myVirtualEnv} with your specific directory names.

0

In case this helps those scratching their heads after reading through all the answers here - it also seems that you might need to re-create your virtualenv in case you created it before installing python3.6-tk. None of the solutions with specifying TK_PATH and TCL_PATH seemed to work, even when using the paths given by tkinter outside the virtual environment (see this post). To be sure, just delete the venv and create a new one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.