更新PYthon3.8 以及Python may not be configured for TK解决

最近开始学习Python,于是安装了ubuntu18.04LTS 并构建系统,本人小白级别,全靠网上大师指点。
在此多谢。

ubuntu18.04LTS自带的python为3.6版本
于是进行了更新3.8版本,更新方法参考:
https://www.linuxidc.com/Linux/2019-11/161448.htm
更新完之后正常使用如下:

~$ python3.8
Python 3.8.1 (default, Feb 23 2020, 13:19:04) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

查看python3.8安装位置

~$ which python3.8
/usr/local/bin/python3.8

我这里没有该python3的指向,故默认python3还是原来的版本

$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

修改指向可参考:
https://blog.csdn.net/u014775723/article/details/85213793
https://blog.csdn.net/nightelf00/article/details/89408658

改默认python3指向,终端会打不开,需要修改gnome-terminal的第一行
默认如下:

sudo gedit  /usr/bin/gnome-terminal
#!/usr/bin/python3
import string
import subprocess
import sys
import random

查看python3的指向

$ ls -l /usr/bin | grep python
lrwxrwxrwx 1 root root          26 3月  27  2018 dh_pypy -> ../share/dh-python/dh_pypy
lrwxrwxrwx 1 root root          29 3月  27  2018 dh_python3 -> ../share/dh-python/dh_python3
lrwxrwxrwx 1 root root          23 2月  23 12:16 pdb3.6 -> ../lib/python3.6/pdb.py
lrwxrwxrwx 1 root root          31 2月  23 12:16 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx 1 root root          26 3月  27  2018 pybuild -> ../share/dh-python/pybuild
lrwxrwxrwx 1 root root           9 2月  23 12:16 python3 -> python3.6 
-rwxr-xr-x 1 root root     4526456 11月  7 18:44 python3.6
lrwxrwxrwx 1 root root          33 11月  7 18:44 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x 1 root root     4526456 11月  7 18:44 python3.6m
lrwxrwxrwx 1 root root          34 11月  7 18:44 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root          16 10月 25  2018 python3-config -> python3.6-config
lrwxrwxrwx 1 root root          10 2月  23 12:16 python3m -> python3.6m
lrwxrwxrwx 1 root root          17 10月 25  2018 python3m-config -> python3.6m-config
lrwxrwxrwx 1 root root          34 11月  7 18:44 x86_64-linux-gnu-python3.6-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x 1 root root        3283 11月  7 18:44 x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx 1 root root          33 10月 25  2018 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.6-config
lrwxrwxrwx 1 root root          34 10月 25  2018 x86_64-linux-gnu-python3m-config -> x86_64-linux-gnu-python3.6m-config

因为python3 -> python3.6 ,所以如果将python3的指向改为3.8后,需要为gnome-terminal修改第一行:

#!/usr/bin/python3.6
import string
import subprocess
import sys
import random

到此正常使用Python3.8 安装VScode ,开始学习。

××××××××××××××××××××××××××××××××××××××××

学习turtle时候出现问题
If this fails your Python may not be configured for Tk
查询需要安装TK以及dev

安装tk
>> sudo apt install python3-tk  (Ubuntu)
>> yum install python3-tk    (Centos)

安装tk开发类库:
>> sudo apt install tk-dev  (Ubuntu)
>> yum install tk-devel    (Centos)

安装之后,python3.6可以正常使用

Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

但是python3.8依然报错

$ python3.8
Python 3.8.1 (default, Feb 23 2020, 13:19:04) 
[GCC 7.4.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/local/lib/python3.8/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
>>> exit()

查找资料,对比自己的安装,发现,TK包已经安装,但是3.8无法调用
已经安装:

~$ sudo apt install python3-tk 
[sudo] wangnan 的密码: 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
python3-tk 已经是最新版 (3.6.9-1~18.04)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。

发现在python3.8 import tkinter 的时去找3.8的库文件/usr/local/lib/python3.8/

而python3.8的 tkinter so文件实际上被安装到了 /usr/lib/python3.8/lib-dynload
将文件复制到/usr/local/lib/python3.8/lib-dynload/下面

/usr/lib/python3.8/lib-dynload$ sudo cp _tkinter.cpython-38-x86_64-linux-gnu.so /usr/local/lib/python3.8/lib-dynload/

之后可以正常import tkinter

~$ python3.8
Python 3.8.1 (default, Feb 23 2020, 13:19:04) 
[GCC 7.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> 

参考:
https://www.jb51.net/article/54153.htm
https://blog.csdn.net/jeryjeryjery/article/details/78865362

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值