6

I am using a virtualenv on a Linux machine. I don' have sudo access so I can use pip only.

I tried:

 pip install python-tk

But this resulted in this error message

Collecting python-tk
  Could not find a version that satisfies the requirement python-tk (from versions: )
No matching distribution found for python-tk

How can I install Tkinter in virtualenv on Linux?

garg10may
  • 5,128
  • 8
  • 42
  • 78

1 Answers1

3

You can't install tkinter using pip because tkinter is an interface to a C++ library called Tk, whereas pip is coded with Python.

Luckily you do not have to worry about the above statement because tkinter comes as a built-in library for the standard Python distribution.

So what you have to do is:

  1. Go to your virtualenv directory: cd to_your_virtualenv_directory
  2. Activate it: source bin/activate
  3. Access your python shell within it: python
  4. Then import tkinter as tk

Note:

Depending on your settings, maybe when you type python you will notice you are prompted to work with Python 2.x instead. In that case, just type this: import Tkinter as Tk. If, however, typing python leads you to use Python 3.x (as I set it on my machine), but you prefer to work with Python 2.x then simply type python2 instead of python.

Nathan
  • 763
  • 1
  • 9
  • 29
Billal Begueradj
  • 17,880
  • 38
  • 105
  • 123
  • I needed to install that since when I import it results in error - No module named tkinter – garg10may Jul 24 '17 at 12:51
  • 1
    In that case, a common solution is to set the `TCL_LIBRARY` environment variable as described [here](https://stackoverflow.com/questions/15884075/tkinter-in-a-virtualenv). Are you on Ubuntu? Which version? Please add such information to your question. – Billal Begueradj Jul 24 '17 at 12:58
  • have also tried `import Tkinter` but that didin't work either. Also tried TCL approach but I don't see any `tcl` library in my installations. – garg10may Jul 25 '17 at 04:39
  • please also see [this](https://stackoverflow.com/questions/4783810/install-tkinter-for-python) answer – Nathan Feb 10 '19 at 19:27
  • 1
    Which answer are you pointing to? None of them suggested to install tkinter the way OP asked, and my answer is correct @frank – Billal Begueradj Feb 10 '19 at 19:34
  • 1
    I'm sorry; you're right. I didn't read the OP's question carefully, so I missed "don't have `sudo` access." If you accept my edit, I will upvote your answer instead of downvoting – Nathan Feb 10 '19 at 21:16
  • I appreciate your intellectual honesty, we all make mistakes so I do not care about downvotes especially if there is an attempt to justify them as you did. @frank – Billal Begueradj Feb 11 '19 at 07:12
  • @BillalBegueradj regardless, I've upvoted. I think a really good software engineer **HAS** to be intellectually honest. Otherwise, there will be bugs. And we would at least like to minimize the number of those, though we can't get rid of them completely :P – Nathan Feb 11 '19 at 15:01