标签归档:amazon-ec2

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

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

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

sudo yum install python3

不起作用:

No package python3 available.

我已经四处搜寻,找不到其他人遇到这个问题,所以我在这里问。我必须手动下载并安装它吗?

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

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?


回答 0

如果你做一个

sudo yum list | grep python3

您会看到,虽然它们没有“ python3”软件包,但确实有“ python34”软件包或更新的发行版,例如“ python36”。安装起来很简单:

sudo yum install python34 python34-pip

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:

sudo yum install python34 python34-pip

回答 1

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

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

sudo amazon-linux-extras install python3

如果要使用它设置隔离的虚拟环境,请执行以下操作:使用yum install‘d virtualenv工具似乎无法可靠地工作。

virtualenv --python=python3 my_venv

调用venv模块/工具不太麻烦,您可以python3 --version事先检查一下它是否是您想要/期望的。

python3 -m venv my_venv

它可以安装的其他东西(截至18 Jan 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 ]

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.

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

sudo amazon-linux-extras install python3

If you want to set up isolated virtual environments with it; using yum install‘d virtualenv tools don’t seem to reliably work.

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 --version beforehand.

python3 -m venv my_venv

Other things it can install (versions as of 18 Jan 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 ]

回答 2

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

请注意,您可能需要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)

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).

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

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)

回答 3

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

sudo yum install python35
sudo yum install python35-pip

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

sudo yum install python35
sudo yum install python35-pip

回答 4

自Amazon Linux版本2017.09起,python 3.6现在可用:

sudo yum install python36 python36-virtualenv python36-pip

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

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

sudo yum install python36 python36-virtualenv python36-pip

See the Release Notes for more info and other packages


回答 5

Amazon Linux现在支持python36。

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

Amazon Linux now supports python36.

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

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

回答 6

正如@NickT所说,Amazon Linux 2的默认yum存储库中没有python3 [4-6]今天为止它使用3.7,在这里查看所有答案,我们可以说它会随着时间的变化而变化。

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

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

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.

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

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

回答 7

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

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

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

sudo yum install centos-release-scl

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

sudo yum install rh-python36

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

scl enable rh-python36 bash

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

python --version

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

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

sudo yum groupinstall Development Tools

现在创建一个虚拟环境,以便不会弄乱默认的python包。

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

要使用此虚拟环境,

source my_project_venv/bin/activate

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

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.

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.

sudo yum install centos-release-scl

Now that we have SCL repository, we can install the 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:

scl enable rh-python36 bash

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

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.

Now, Install the python development tools by typing:

sudo yum groupinstall ‘Development Tools’

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

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.


回答 8

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

sudo apt-get install python3

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

sudo yum install python36

在SUSE及其衍生物上,请使用zypper。检查存储库中可用的Python版本。然后。运行类似于以下内容的命令,以替换正确的软件包名称:

sudo zypper install python3

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:

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:

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:

sudo zypper install python3