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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
garg10may
  • 5,794
  • 11
  • 50
  • 91
  • Does this answer your question? [Why does tkinter seem to be missing or broken? Shouldn't it be part of the standard library?](https://stackoverflow.com/questions/76105218/why-does-tkinter-seem-to-be-missing-or-broken-shouldnt-it-be-part-of-the-stand) – Karl Knechtel Apr 25 '23 at 20:56

1 Answers1

2

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 majicvr.com
  • 950
  • 2
  • 11
  • 31
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • 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 majicvr.com 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 majicvr.com 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 majicvr.com Feb 11 '19 at 15:01
  • "You can't install tkinter using pip because tkinter is an interface to a C++ library called Tk, whereas pip is coded with Python." This explanation is wrong, and nonsensical. Many popular Python libraries interface to C or C++ code, and the fact that *pip itself* was coded in Python has *nothing to do with* the software *that it installs*. The actual reason you can't install tkinter using Pip is because tkinter - being part of the standard library - is not made available through PyPI. Pip is for installing third-party content, which Tkinter isn't. – Karl Knechtel Apr 29 '23 at 00:40
  • When Tkinter is missing from a Python installation, and is subsequently installed (for example by using the system package manager), this will generally add both Python code and a compiled C library to the system. There are at least three layers: the Tcl/Tk library (written in C and specifically intended to be used from other C code), the `_tkinter` library (also written in C, but which can technically be `import`ed from Python), and the `tkinter` package (several Python files which use `_tkinter` under the hood). – Karl Knechtel Apr 29 '23 at 00:42
  • Aside from that, this answer amounts to "Q. how do I install it in a virtualenv? A. You don't need to" - and that will *only work if the base Python that the virtualenv is based upon* includes Tkinter. Many don't. If OP does not have sudo access, it will not be possible to modify the system Python to add Tkinter, and therefore will not be possible to create virtual environments from that Python which support Tkinter. Instead, a separate Python must be installed from source, in whatever way is possible with user-level privileges. – Karl Knechtel Apr 29 '23 at 00:45