为不同的python版本安装tkinter- arch linux

7dl7o3gd  于 2022-12-11  发布在  Linux
关注(0)|答案(1)|浏览(219)

我的系统主Python版本是3.10.8,虽然我需要3.9版本的一些项目,所以我安装了它使用sudo pacman -S python39
我需要tkinter用于某个项目,而系统已经安装了它:
pacman -Ss tk

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

如果我尝试在默认的python版本中导入tkinter,则没有问题:

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()

但是,在尝试导入不同python版本的tkinter时,我看到以下问题:

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()

因为tk在python 3.9版中是不可见的。
我尝试在tk中搜索其他python版本,但是pacman没有找到任何可用的包:

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
brgchamk

brgchamk1#

我通过使用pyenv管理多个python版本来解决这个问题:

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

然后将这些行添加到~/.bashrc

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

最后:

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()

相关问题