如何在 AWS EC2 实例上安装 Python 3?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27669927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:05:53  来源:igfitidea点击:

How do I install Python 3 on an AWS EC2 instance?

pythonpython-3.xamazon-web-servicesamazon-ec2

提问by Jake_Howard

I'm trying to install python 3.x on an AWS EC2 instance and:

我正在尝试在 AWS EC2 实例上安装 python 3.x 并且:

sudo yum install python3

doesn't work:

不起作用:

No package python3 available.

I've googled around and I can't find anyone else who has this problem so I'm asking here. Do I have to manually download and install it?

我已经用谷歌搜索了,我找不到其他有这个问题的人,所以我在这里问。我必须手动下载并安装它吗?

采纳答案by TaborKelly

If you do a

如果你做一个

sudo yum list | grep python3

you will see that while they don't have a "python3" package, they do have a "python34" package, or a more recent release, such as "python36". Installing it is as easy as:

你会看到,虽然他们没有“python3”包,但他们有“python34”包,或者更新的版本,比如“python36”。安装它就像:

sudo yum install python34 python34-pip

回答by Jake_Howard

Here are the steps I used to manually install python3 for anyone else who wants to do it as it's not super straight forward. EDIT:It's almost certainly easier to use the yum package manager (see other answers).

以下是我用来为任何想要安装 python3 的人手动安装 python3 的步骤,因为它不是超级简单的。编辑:使用 yum 包管理器几乎肯定更容易(请参阅其他答案)。

Note, you'll probably want to do sudo yum groupinstall 'Development Tools'before doing this otherwise pip won't install.

请注意,您可能需要sudo yum groupinstall 'Development Tools'在执行此操作之前执行此操作,否则 pip 将无法安装。

wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz
tar zxvf Python-3.4.2.tgz
cd Python-3.4.2
sudo yum install gcc
./configure --prefix=/opt/python3
make
sudo yum install openssl-devel
sudo make install
sudo ln -s /opt/python3/bin/python3 /usr/bin/python3
python3 (should start the interpreter if it's worked (quit() to exit)

回答by Russ McFatter

EC2 (on the Amazon Linux AMI) currently supports python3.4 and python3.5.

EC2(在 Amazon Linux AMI 上)目前支持 python3.4 和 python3.5。

sudo yum install python35
sudo yum install python35-pip

回答by Realistic

As of Amazon Linux version 2017.09 python 3.6 is now available:

从 Amazon Linux 2017.09 版本开始,python 3.6 现已可用:

sudo yum install python36 python36-virtualenv python36-pip

See the Release Notesfor more info and other packages

有关更多信息和其他软件包,请参阅发行说明

回答by Nick T

Note: This may be obsolete for current versions of Amazon Linux 2 since late 2018 (see comments), you can now directly install it via yum install python3.

注意:自 2018 年末以来,这对于当前版本的 Amazon Linux 2 可能已过时(请参阅评论),您现在可以通过yum install python3.

In Amazon Linux 2, there isn't a python3[4-6]in the default yum repos, instead there's the Amazon Extras Library.

在 Amazon Linux 2 中python3[4-6]默认 yum 存储库中没有,而是Amazon Extras 库

sudo amazon-linux-extras install python3

If you want to set up isolated virtual environments with it; using yum install'd virtualenvtools don't seem to reliably work.

如果你想用它设置隔离的虚拟环境;使用yum install'dvirtualenv工具似乎不能可靠地工作。

virtualenv --python=python3 my_venv
virtualenv --python=python3 my_venv

Calling the venv module/tool is less finicky, and you could double check it's what you want/expect with python3 --versionbeforehand.

调用 venv 模块/工具不那么挑剔,您可以python3 --version事先仔细检查它是否是您想要/期望的。

python3 -m venv my_venv


Other things it can install (versions as of 18 Jan 18):

它可以安装的其他东西(截至 18 年 1 月 18 日的版本):

[ec2-user@x ~]$ amazon-linux-extras list
  0  ansible2   disabled  [ =2.4.2 ]
  1  emacs   disabled  [ =25.3 ]
  2  memcached1.5   disabled  [ =1.5.1 ]
  3  nginx1.12   disabled  [ =1.12.2 ]
  4  postgresql9.6   disabled  [ =9.6.6 ]
  5  python3=latest  enabled  [ =3.6.2 ]
  6  redis4.0   disabled  [ =4.0.5 ]
  7  R3.4   disabled  [ =3.4.3 ]
  8  rust1   disabled  [ =1.22.1 ]
  9  vim   disabled  [ =8.0 ]
 10  golang1.9   disabled  [ =1.9.2 ]
 11  ruby2.4   disabled  [ =2.4.2 ]
 12  nano   disabled  [ =2.9.1 ]
 13  php7.2   disabled  [ =7.2.0 ]
 14  lamp-mariadb10.2-php7.2   disabled  [ =10.2.10_7.2.0 ]

回答by Sairam Krish

Amazon Linux now supports python36.

Amazon Linux 现在支持 python36。

python36-pip is not available. So need to follow a different route.

python36-pip 不可用。所以需要走不同的路线。

sudo yum install python36 python36-devel python36-libs python36-tools

# If you like to have pip3.6:
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

回答by Putnik

As @NickT said, there's no python3[4-6] in the default yum repos in Amazon Linux 2, as of today it uses 3.7 and looking at all answers here we can say it will be changed over time.

正如@NickT 所说,Amazon Linux 2的默认 yum 存储库中没有 python3[4-6] ,截至今天,它使用 3.7 并查看此处的所有答案,我们可以说它会随着时间的推移而改变。

I was looking for python3.6 on Amazon Linux 2 but amazon-linux-extrasshows a lot of options but no python at all. in fact, you can try to find the version you know in epelrepo:

我在 Amazon Linux 2 上寻找 python3.6,但amazon-linux-extras显示了很多选项,但根本没有 python。其实你可以尝试在epelrepo中找到你知道的版本:

sudo amazon-linux-extras install epel

yum search python | grep "^python3..x8"

python34.x86_64 : Version 3 of the Python programming language aka Python 3000
python36.x86_64 : Interpreter of the Python programming language

回答by nambastha

Adding to all the answers already available for this question, I would like to add the steps I followed to install Python3 on AWS EC2 instance running CentOS 7. You can find the entire details at this link.

除了此问题的所有可用答案之外,我想添加在运行 CentOS 7 的 AWS EC2 实例上安装 Python3 所遵循的步骤。您可以在此链接中找到完整的详细信息。

https://aws-labs.com/install-python-3-centos-7-2/

https://aws-labs.com/install-python-3-centos-7-2/

First, we need to enable SCL. SCL is a community project that allows you to build, install, and use multiple versions of software on the same system, without affecting system default packages.

首先,我们需要启用 SCL。SCL 是一个社区项目,允许您在同一系统上构建、安装和使用多个版本的软件,而不会影响系统默认包。

sudo yum install centos-release-scl

Now that we have SCL repository, we can install the python3

现在我们有了 SCL 存储库,我们可以安装 python3

sudo yum install rh-python36

To access Python 3.6 you need to launch a new shell instance using the Software Collection scl tool:

要访问 Python 3.6,您需要使用 Software Collection scl 工具启动一个新的 shell 实例:

scl enable rh-python36 bash

If you check the Python version now you'll notice that Python 3.6 is the default version

如果您现在检查 Python 版本,您会注意到 Python 3.6 是默认版本

python --version

It is important to point out that Python 3.6 is the default Python version only in this shell session. If you exit the session or open a new session from another terminal Python 2.7 will be the default Python version.

重要的是要指出 Python 3.6 是仅在此 shell 会话中的默认 Python 版本。如果您退出会话或从另一个终端打开一个新会话,Python 2.7 将是默认的 Python 版本。

Now, Install the python development tools by typing:

现在,输入以下命令安装 python 开发工具:

sudo yum groupinstall ‘Development Tools'

Now create a virtual environment so that the default python packages don't get messed up.

现在创建一个虚拟环境,这样默认的 python 包就不会搞砸了。

mkdir ~/my_new_project
cd ~/my_new_project
python -m venv my_project_venv

To use this virtual environment,

要使用这个虚拟环境,

source my_project_venv/bin/activate

Now, you have your virtual environment set up with python3.

现在,您已经使用 python3 设置了虚拟环境。

回答by Light Bringer

On Debian derivatives such as Ubuntu, use apt. Check the apt repository for the versions of Python available to you. Then, run a command similar to the following, substituting the correct package name:

在 Ubuntu 等 Debian 衍生产品上,使用 apt。检查 apt 存储库以获取您可用的 Python 版本。然后,运行类似于以下内容的命令,替换正确的包名称:

sudo apt-get install python3

On Red Hat and derivatives, use yum. Check the yum repository for the versions of Python available to you. Then, run a command similar to the following, substituting the correct package name:

在 Red Hat 和衍生产品上,使用 yum。检查 yum 存储库以获取您可用的 Python 版本。然后,运行类似于以下内容的命令,替换正确的包名称:

sudo yum install python36

On SUSE and derivatives, use zypper. Check the repository for the versions of Python available to you. Then. run a command similar to the following, substituting the correct package name:

在 SUSE 和衍生产品上,使用 zypper。检查存储库中可用的 Python 版本。然后。运行类似于以下的命令,替换正确的包名称:

sudo zypper install python3