1

I am running Linux Mint 20.3 Cinnamon, and I have some virtual environments created (using the standard library venv module) from the system Python (no other Python installations), which is version 3.8.10. I know that the system Python was configured without TKinter support:

>>> import _tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named '_tkinter'
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> import turtle
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/turtle.py", line 107, in <module>
    import tkinter as TK
ModuleNotFoundError: No module named 'tkinter'

Curiously, the turtle module is present (and fails to import because tkinter is missing) even though tkinter is not (and would fail to import because _tkinter is missing).

I understand that I should be able to make TKinter available globally by installing the python3-tk system package; and I understand that TKinter depends on non-Python code that needs to be installed anyway (and not on a per-venv basis).

However: to my understanding, installing python3-tk will, as well as installing the relevant TCL/Tk code, update the system Python to include the tkinter standard library package. On principle, I would like to avoid this. I infer that the absence of this package is a deliberate choice by the Mint team (especially considering that it is even absent in Cinnamon) with positive security implications (a rogue Python script would not be able to exploit a hypothetical Tcl/Tk vulnerability discovered in the future, except within a user-level venv).

It does appear that my system already has that TCL code installed, anyway:

$ apt list tcl
Listing... Done
tcl/focal,now 8.6.9+1 amd64 [installed]
tcl/focal 8.6.9+1 i386

Is it possible to configure my system such that

  • the core TCL/Tk code is available and usable from Python virtual environments;
  • each new virtualenv that I create can get Python bindings (i.e., the tkinter standard library package) to that code from some standard source; but
  • the system Python is not modified (and fails-by-design to run Tkinter GUI code)?

If so, how?

And what exactly does python3-tk propose to install, anyway?

3
  • The only way that could be done is depending on the packages maintainers (eg: if they make it available on pip too instead of separate to Python installation). Symlinking instead of copying the libs works too, as seen on this SO answer: stackoverflow.com/a/37391706/12349101 . Lastly you can of course compile Python from sources, and make it work inside a specific directory instead of installing it globally (eg: like a virtualenv does), but it might break some existing (Python) application if done wrong. Commented Mar 6, 2023 at 21:16
  • 1
    @NordineLotfi Sounds like the start of an answer to me. Knowing what isn't possible is also helpful in my circumstance. Commented Mar 6, 2023 at 23:04
  • Tried to add a bit more to what I said earlier. Feedback welcome Commented Mar 7, 2023 at 8:53

2 Answers 2

1

you can see the contents of the python3-tk package here: https://ubuntu.pkgs.org/22.04/ubuntu-updates-main-amd64/python3-tk_3.10.8-1~22.04_amd64.deb.html (or download it off apt directly)

you can also download the .deb and extract it with ar x

it just contains the tkinter python module. my guess is that if you just pasted that into your venv's site-packages folder it would work, but i cant test as i already have tkinter installed on my computer

0

The only way that could be done is depending on the packages maintainers (eg: if they make it available on pip too instead of separate to Python installation). virtualenv and pip goes hand in hand together.

Symlinking instead of copying the libs (and relevant files) works too, as seen on this SO answer.

Lastly you can of course compile Python from sources, and make it work inside a specific directory instead of installing it globally (eg: like a virtualenv does), but it might break some existing (Python) application if done wrong (eg:this is what projects like homebrew/linuxbrew does).

A good existing solution for that would be pyenv. Note that it will still compile from sources but will do so in a single directory (either specified, or in a dot directory in $HOME). It won't act as a replacement to virtualenv but it will prevent the system's files from being edited (since it does not copy the compiled binaries globally/system-wide).

5
  • 1
    "Symlinking instead of copying the libs (and relevant files) works too, as seen on this SO answer." It would, but that Q&A is addressing a different problem ("updating" a venv that was created before doing a system install of python3-tk, and/or fixing a separately-installed Python). It wouldn't help with keeping the changes out of the system Python. There's some good and useful information here, though. I think the first paragraph is key, and is the conclusion I've come to as well, at least for now. Commented Mar 7, 2023 at 14:59
  • 1
    I think I might also ask about this on the CPython issue tracker. Commented Mar 7, 2023 at 15:00
  • That's true, I just felt it was related since usually I think virtualenv is built by copying files instead of symlinking. One way to still make it relevant is using this with pyenv, but it might be just going around the existing problem instead of fixing it. I think asking on the official issue tracker would be interesting yeah @KarlKnechtel Commented Mar 7, 2023 at 15:01
  • 1
    Now that I think about it, though... how exactly does package installation work? Are absolute paths hard-coded within the package itself, or would they be relative to some other system config? Maybe it would be possible to temporarily do something like what packages like pythonispython3 do, before installing python3-tk, in order to tell the installer that the venv is the "system Python". Commented Mar 7, 2023 at 15:05
  • That's a good question. I guess this could make for a good canonical on SO :o @KarlKnechtel My wild guess is you could probably do that using either 1. symlinks, 2. copying relevant files/directory, 3. modifying PATH variables. Another way would be to do something a bit more python related. I think APT has a python api... Commented Mar 7, 2023 at 15:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.