前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >在Ubuntu 18.04 LTS上安装Python 3.7

在Ubuntu 18.04 LTS上安装Python 3.7

作者头像
用户6543014
发布2020-04-02 11:35:52
6K0
发布2020-04-02 11:35:52
举报
文章被收录于专栏:CU技术社区CU技术社区

Python是最受欢迎和随需应变的通用编程语言之一。它是一种解释性的高级编程语言,支持多种编程范例,包括过程式,面向对象和函数式编程。由于其全面的标准库,它通常被描述为“py自带着电池”。

它被广泛使用,并且是一种非常通用的编程语言,因为它被初学者和科学家用于不同类型的活动。它用于系统编程和脚本编写、重复任务自动化、构建网站和Web应用程序、机器学习算法、数据分析等等。

在本教程中,我们将学习如何在Ubuntu 18.04 LTS Bionic Beaver中安装Python 3.7。我们将包括在Ubuntu 18.04机器上安装它的两种主要方法,一种是通过deadsnake PPA使用APT包管理器,另一种是通过源代码编译。

使用APT(和PPA)在ubuntu上安装Python 3 .7

默认情况下,Ubuntu 18.04随附Python 3.6.9。通过运行以下命令,您可以获得python版本:

代码语言:javascript
复制
python3 --version
Python 3.6.9

python3-版本Python 3.6.9首先,我们需要确保更新Ubuntu 18.04计算机的Ubuntu软件包存储库索引。这可以通过执行以下命令来完成:

代码语言:javascript
复制
$ sudo apt update

更新更新完成后,我们需要确保在计算机上安装了“ software-properties-common”软件包,以便可以将PPA添加到计算机中。

代码语言:javascript
复制
$ sudo apt install software-properties-common

现在,我们可以使用以下命令轻松添加Deadsnakes PPA:

代码语言:javascript
复制
$ sudo add-apt-repository ppa:deadsnakes/ppa

最后,将必需的PPA添加到我们的Ubuntu 18.04计算机中后,我们现在可以简单地使用apt命令安装Python 3.7。

apt notify命令中的-y选项可将自动yes选项设置为提示。

代码语言:javascript
复制
$ sudo apt install python3.7 -y
代码语言:javascript
复制
Output
$ sudo apt install python3.7 -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython3.7-minimal libpython3.7-stdlib python3-distutils python3-lib2to3
  python3.7-minimal
Suggested packages:
  python3.7-venv python3.7-doc binfmt-support
The following NEW packages will be installed:
  libpython3.7-minimal libpython3.7-stdlib python3-distutils python3-lib2to3
  python3.7 python3.7-minimal
0 upgraded, 6 newly installed, 0 to remove and 321 not upgraded.
Need to get 4,792 kB of archives.
After this operation, 26.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 libpython3.7-minimal amd64 3.7.7-1+bionic1 [596 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-lib2to3 all 3.6.9-1~18.04 [77.4 kB]
Get:3 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 python3.7-minimal amd64 3.7.7-1+bionic1 [1,839 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-distutils all 3.6.9-1~18.04 [144 kB]
Get:5 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 libpython3.7-stdlib amd64 3.7.7-1+bionic1 [1,784 kB]
Get:6 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic/main amd64 python3.7 amd64 3.7.7-1+bionic1 [351 kB]
Fetched 4,792 kB in 7s (737 kB/s)                                              
Selecting previously unselected package libpython3.7-minimal:amd64.
(Reading database ... 134446 files and directories currently installed.)
Preparing to unpack .../0-libpython3.7-minimal_3.7.7-1+bionic1_amd64.deb ...
Unpacking libpython3.7-minimal:amd64 (3.7.7-1+bionic1) ...
Selecting previously unselected package python3.7-minimal.
Preparing to unpack .../1-python3.7-minimal_3.7.7-1+bionic1_amd64.deb ...
Unpacking python3.7-minimal (3.7.7-1+bionic1) ...
Selecting previously unselected package libpython3.7-stdlib:amd64.
Preparing to unpack .../2-libpython3.7-stdlib_3.7.7-1+bionic1_amd64.deb ...
Unpacking libpython3.7-stdlib:amd64 (3.7.7-1+bionic1) ...
Selecting previously unselected package python3-lib2to3.
Preparing to unpack .../3-python3-lib2to3_3.6.9-1~18.04_all.deb ...
Unpacking python3-lib2to3 (3.6.9-1~18.04) ...
Selecting previously unselected package python3-distutils.
Preparing to unpack .../4-python3-distutils_3.6.9-1~18.04_all.deb ...
Unpacking python3-distutils (3.6.9-1~18.04) ...
Selecting previously unselected package python3.7.
Preparing to unpack .../5-python3.7_3.7.7-1+bionic1_amd64.deb ...
Unpacking python3.7 (3.7.7-1+bionic1) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ...
Setting up libpython3.7-minimal:amd64 (3.7.7-1+bionic1) ...
Setting up python3.7-minimal (3.7.7-1+bionic1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ...
Setting up python3-lib2to3 (3.6.9-1~18.04) ...
Setting up python3-distutils (3.6.9-1~18.04) ...
Setting up libpython3.7-stdlib:amd64 (3.7.7-1+bionic1) ...
Setting up python3.7 (3.7.7-1+bionic1) ...

现在,为了确保python是否成功安装,我们可以检查下python版本。

代码语言:javascript
复制
$ python3.7 -V
Python 3.7.7

从源代码在ubuntu上安装Python 3.7

在这种方法中,我们将学习如何通过从源代码编译Python 3.7在Ubuntu 18.04计算机中安装它。如果要继续进行,我们需要确保满足某些先决条件。首先,我们需要确保编译源代码所需的构建工具已安装在我们的计算机中。

为此,我们首先需要更新apt的本地存储库索引,如下所示:

代码语言:javascript
复制
$ sudo apt update

随着本地存储库索引的更新,我们现在将安装编译和安装python3.7所需的依赖项。为此,我们需要执行以下命令:

代码语言:javascript
复制
$ sudo apt install -y build-essential wget zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

一旦所有依赖项都已安装并准备就绪,我们将使用wget命令从官方python网站下载Python 3.7的最新版本,如下所示:

代码语言:javascript
复制
$ wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
--2020-03-17 16:19:44--  https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tgz
Resolving www.python.org (www.python.org)... 151.101.8.223, 2a04:4e42:2::223
Connecting to www.python.org (www.python.org)|151.101.8.223|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23161893 (22M) [application/octet-stream]
Saving to: ‘Python-3.7.7.tgz’

Python-3.7.7.tgz    100%[===================>]  22.09M  2.50MB/s    in 9.2s

2020-03-17 16:19:54 (2.41 MB/s) - ‘Python-3.7.7.tgz’ saved [23161893/23161893]

在撰写本教程的过程中,python 3.7的发布是在最新版本3.7.7下发布的。因此,我们将仅使用tar命令下载并提取tgz文件。

代码语言:javascript
复制
$ tar -xf Python-3.7.7.tgz

从tgz文件中提取源代码之后,我们现在将cd进入目录,然后执行configure脚本以评估是否满足了编译所需的依赖项关系。启用优化标志优化python二进制文件并将代码执行增加10-20%。

代码语言:javascript
复制
$ cd Python-3.7.7
代码语言:javascript
复制
$ ./configure --enable-optimizations
checking if the dirent structure of a d_type field... yes
checking for the Linux getrandom() syscall... yes
checking for the getrandom() function... yes
checking for pkg-config... no
checking for openssl/ssl.h in /usr/local/ssl... no
checking for openssl/ssl.h in /usr/lib/ssl... no
checking for openssl/ssl.h in /usr/ssl... no
checking for openssl/ssl.h in /usr/pkg... no
checking for openssl/ssl.h in /usr/local... no
checking for openssl/ssl.h in /usr... yes
checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... yes
checking for --with-ssl-default-suites... python
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

成功执行configure脚本后,我们现在将通过使用带有altinstall参数的make命令在Ubuntu 18.04 LTS计算机中安装python 3.7。altinstall参数可确保未将其安装为系统默认的python版本。

代码语言:javascript
复制
$ sudo make altinstall
changing mode of /usr/local/bin/idle3.7 to 755
changing mode of /usr/local/bin/pyvenv-3.7 to 755
changing mode of /usr/local/bin/pydoc3.7 to 755
changing mode of /usr/local/bin/2to3-3.7 to 755
rm /usr/local/lib/python3.7/lib-dynload/_sysconfigdata_m_linux_x86_64-linux-gnu.py
rm -r /usr/local/lib/python3.7/lib-dynload/__pycache__
Creating directory /usr/local/share/man/man1
/usr/bin/install -c -m 644 ./Misc/python.man \
  /usr/local/share/man/man1/python3.7.1
if test "xupgrade" != "xno"  ; then \
  case upgrade in \
    upgrade) ensurepip="--altinstall --upgrade" ;; \
    install|*) ensurepip="--altinstall" ;; \
  esac; \
   ./python -E -m ensurepip \
    $ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmp06ih2lq9
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-19.2.3 setuptools-41.2.0

现在,我们可以通过如下检查其版本来检查python 3.7是否已成功安装。

代码语言:javascript
复制
$ python3.7 -V
Python 3.7.7

结论

最后,我们已在Ubuntu 18.04 LTS计算机上成功安装了python 3.7。在撰写本教程时,Python 3.7的最新版本为3.7.7,因此您可能希望将3.7.7版本替换为该发行版的最新版本。现在,我们可以轻松执行为3.7编写的python脚本和代码。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 SACC开源架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档