当前位置:   article > 正文

ubuntu安装python3.6_Ubuntu 16.04上安装和使用Python3.6

libpython3.6-stdlib : 依赖: libpython3.6-minimal (= 3.6.9-1~18.04ubuntu1.12)

这两天抽空开始跟着 Learn Python 3 The Hard Way 学习 python 3, 当做到第5个练习的时候,出现了如下错误:

david@KingChef-Workstation:~/learnpy$ python3 ex5.py

File "ex5.py", line 9

print(f"Let's talk about {my_name}.")

^

SyntaxError: invalid syntax

再三检查发现代码没有问题,但运行就是出错。听说Python各版本之间会有差异,会不会是因为Python版本本身的问题呢?

david@KingChef-Workstation:~$python -V

Python 2.7.12

david@KingChef-Workstation:~$python3 -V

Python 3.5.2

Ubuntu 16.04 默认安装的 Python 版本是2.7和3.5,而 Learn Python 3 The Hard Way 里面用的是 Python 3.6。大伟哥查了一下,了解到 F-string 正好就是 Python 3.6 的新特性之一。要继续学习的话,就需要安装 Python 3.6 了,好在Ubuntu下面可以很方便的让多个 Python 版本同时存在,只要在运行的时候指定 Python 的版本号就可以了。

要在 Ubuntu 16.04 上面安装 Python 3.6 需要添加python 3.6的源:

david@KingChef-Workstation:~$sudo add-apt-repository ppa:jonathonf/python-3.6

[sudo] password for david:

A plain backport of *just* Python 3.6. System extensions/Python libraries may or may not work.

Don't remove Python 3.5 from your system - it will break.

More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6

Press [ENTER] to continue or ctrl-c to cancel adding it

这里提示我们安装 Python 3.6 后不要删除 Python 3.5,不然的话系统会崩溃,因为 Linux 系统里面对 Pyhon 的依赖是比较多的,网上能查到很多人吐槽自己删除系统默认的 Python 版本后出现问题,不得不重新装回原来的版本,甚至重装系统的经历。我们根据提示键入 Enter 后,把 Python 3.6 的PPA源正式加入到源列表:

gpg: keyring `/tmp/tmpsnw0vrl9/secring.gpg' created

gpg: keyring `/tmp/tmpsnw0vrl9/pubring.gpg' created

gpg: requesting key F06FC659 from hkp server keyserver.ubuntu.com

gpg: /tmp/tmpsnw0vrl9/trustdb.gpg: trustdb created

gpg: key F06FC659: public key "Launchpad PPA for J Fernyhough" imported

gpg: Total number processed: 1

gpg: imported: 1 (RSA: 1)

OK

确认无误后,运行apt update更新资源列表,然后安装 Python 3.6:

$ sudo apt update

$ sudo apt install python3.6

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following additional packages will be installed:

libpython3.6-minimal libpython3.6-stdlib python3.6-minimal

Suggested packages:

python3.6-venv python3.6-doc

The following NEW packages will be installed:

libpython3.6-minimal libpython3.6-stdlib python3.6 python3.6-minimal

0 upgraded, 4 newly installed, 0 to remove and 1 not upgraded.

Need to get 4,505 kB of archives.

After this operation, 23.1 MB of additional disk space will be used.

Do you want to continue? [Y/n]

因为源文件在国外,下载软件的过程可能会超时,如果出错,可以重新运行安装命令直到安装完成。完成后可以通过查看 Python 版本来确认是否能够安装成功:

$ python -V

Python 2.7.12

$ python3 -V

Python 3.5.2

$ python3.5 -V

Python 3.5.2

$ python3.6 -V

Python 3.6.5

几个命令下来我们看到,默认的版本还是2.7,Python3 的默认版本还是3.5.2,如果要用 Python 3.6 运行.py脚本的话,需要直接指定 Python 版本:

~/learnpy$ python3 ex5.py

File "ex5.py", line 9

print(f"Let's talk about {my_name}.")

^

SyntaxError: invalid syntax

~/learnpy$ python3.6 ex5.py

Let's talk about David Wei.

He's 170cm tall.

He's 68kg heavy.

Actually that's not too heavy.

He's got Black eyes and Black hair.

His teeth are usually White depending on the coffee.

If I add 36, 170, and 68 I get 274.

如果需要把 Python 3.6 设置为默认版本,可以参考这里:更改Ubuntu默认python版本的两种方法

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/289618
推荐阅读
相关标签
  

闽ICP备14008679号