Ubuntu 16.04上安装和使用Python3.6

大伟哥 提交于 2018-08-24,周五, 18:36

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

  1. david@KingChef-Workstation:~/learnpy$ python3 ex5.py
  2. File "ex5.py", line 9
  3. print(f"Let's talk about {my_name}.")
  4. ^
  5. SyntaxError: invalid syntax


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

  1. david@KingChef-Workstation:~$ python -V
  2. Python 2.7.12
  3. david@KingChef-Workstation:~$ python3 -V
  4. 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的源:

  1. david@KingChef-Workstation:~$ sudo add-apt-repository ppa:jonathonf/python-3.6
  2. [sudo] password for david:
  3. A plain backport of *just* Python 3.6. System extensions/Python libraries may or may not work.
  4.  
  5. Don't remove Python 3.5 from your system - it will break.
  6. More info: https://launchpad.net/~jonathonf/+archive/ubuntu/python-3.6
  7. 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:

  1. $ sudo apt update
  2. $ sudo apt install python3.6
  3. Reading package lists... Done
  4. Building dependency tree
  5. Reading state information... Done
  6. The following additional packages will be installed:
  7. libpython3.6-minimal libpython3.6-stdlib python3.6-minimal
  8. Suggested packages:
  9. python3.6-venv python3.6-doc
  10. The following NEW packages will be installed:
  11. libpython3.6-minimal libpython3.6-stdlib python3.6 python3.6-minimal
  12. 0 upgraded, 4 newly installed, 0 to remove and 1 not upgraded.
  13. Need to get 4,505 kB of archives.
  14. After this operation, 23.1 MB of additional disk space will be used.
  15. Do you want to continue? [Y/n]

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

  1. $ python -V
  2. Python 2.7.12
  3. $ python3 -V
  4. Python 3.5.2
  5. $ python3.5 -V
  6. Python 3.5.2
  7. $ python3.6 -V
  8. Python 3.6.5

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

  1. ~/learnpy$ python3 ex5.py
  2. File "ex5.py", line 9
  3. print(f"Let's talk about {my_name}.")
  4. ^
  5. SyntaxError: invalid syntax
  6. ~/learnpy$ python3.6 ex5.py
  7. Let's talk about David Wei.
  8. He's 170cm tall.
  9. He's 68kg heavy.
  10. Actually that's not too heavy.
  11. He's got Black eyes and Black hair.
  12. His teeth are usually White depending on the coffee.
  13. If I add 36, 170, and 68 I get 274.

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

文章分类

评论

添加新评论

此字段内容将保密,不会被其他人看见。

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。