Home > Net >  Install tkinter for a different python version - arch linux
Install tkinter for a different python version - arch linux

Time:12-08

My system main python version is 3.10.8, although I neeed 3.9 version for some project, so I installed it using sudo pacman -S python39.
I require tkinter for a certain project, and the system already has it installed:

pacman -Ss tk:

extra/tk 8.6.13-1 [installed]
    A windowing toolkit for use with tcl

There is no issue if I attempt to import tkinter in the default python version:

python
Python 3.10.8 (main, Nov  1 2022, 14:18:21) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> quit()

However, I see the following problem when attempting to import tkinter with the different python version:

python39
Python 3.9.15 (main, Nov 17 2022, 21:04:43)
[GCC 12.2.0] on linux
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/python3.9/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
>>> quit()

As tk would not be "seeable" by python version 3.9.
I tried searching tk for other python versions, but pacman doesn't find any available package:

pacman -Ss python-tk
pacman -Ss python3-tk
pacman -Ss python39-tk
pacman -Ss python3.9-tk
pacman -Ss python-tkinter
pacman -Ss python3-tkinter
pacman -Ss python39-tkinter
pacman -Ss python3.9-tkinter

CodePudding user response:

I solved by using pyenv to manage multiple python versions:

sudo pacman -S pyenv python-virtualenv
git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

Then added these lines to ~/.bashrc:

export PATH="/home/andrea/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

And finally:

pyenv install 3.9.15
pyenv virtualenv 3.9.15 my_virtual_environment
source ~/.pyenv/versions/my_virtual_environment/bin/activate.fish
python
Python 3.9.15 (main, Dec  5 2022, 15:02:48)
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> quit()
  • Related