在RHEL上安装Python 3

问题:在RHEL上安装Python 3

我正在尝试使用以下步骤在RHEL上安装python3:

yum search python3

哪个回来了 No matches found for: python3

其次是:

yum search python

搜索结果均未包含python3。接下来我应该尝试什么?

I’m trying to install python3 on RHEL using the following steps:

yum search python3

Which returned No matches found for: python3

Followed by:

yum search python

None of the search results contained python3. What should I try next?


回答 0

手动安装很容易:

  1. 下载(Python.org上可能有较新的版本):

    $ wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz
  2. 解压缩

    $ tar xf Python-3.* 
    $ cd Python-3.*
  3. 准备编译

    $ ./configure
  4. 建立

    $ make
  5. 安装

    $ make install

    或者,如果您不想覆盖python可执行文件(更安全,至少在某些发行版中yum需要python为2.x,例如RHEL6)-您可以python3.*使用并发实例安装到系统默认值altinstall

    $ make altinstall

现在,如果要使用备用安装目录,则可以传递--prefixconfigure命令。

示例:要在/ opt / local中“安装” Python,只需添加--prefix=/opt/local

在后make install步:为了使用新的Python安装,它可能是,你还是要在[前缀] / bin加入到$PATH和[前缀] / lib下的$LD_LIBRARY_PATH(根据的--prefix你通过)

It is easy to install it manually:

  1. Download (there may be newer releases on Python.org):

    $ wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz
    
  2. Unzip

    $ tar xf Python-3.* 
    $ cd Python-3.*
    
  3. Prepare compilation

    $ ./configure
    
  4. Build

    $ make
    
  5. Install

    $ make install
    

    OR if you don’t want to overwrite the python executable (safer, at least on some distros yum needs python to be 2.x, such as for RHEL6) – you can install python3.* as a concurrent instance to the system default with an altinstall:

    $ make altinstall
    

Now if you want an alternative installation directory, you can pass --prefix to the configurecommand.

Example: for ‘installing’ Python in /opt/local, just add --prefix=/opt/local.

After the make install step: In order to use your new Python installation, it could be, that you still have to add the [prefix]/bin to the $PATH and [prefix]/lib to the $LD_LIBRARY_PATH (depending of the --prefix you passed)


回答 1

从RPM安装通常更好,因为:

  • 您可以安装和卸载(正确)python3。
  • 安装时间的方式更快。如果您在具有多个VM的云环境中工作,则不能在每个VM上编译python3。

解决方案1:Red Hat和EPEL存储库

红帽通过EPEL存储库添加了:

  • 适用于CentOS 6的Python 3.4
  • 适用于CentOS 7的Python 3.6

[EPEL]如何在CentOS 6上安装Python 3.4

sudo yum install -y epel-release
sudo yum install -y python34

# Install pip3
sudo yum install -y python34-setuptools  # install easy_install-3.4
sudo easy_install-3.4 pip

您可以使用以下命令创建您的virtualenvpyvenv

pyvenv /tmp/foo

[EPEL]如何在CentOS 7上安装Python 3.6

与CentOS7一起pip3.6提供:)

sudo yum install -y epel-release
sudo yum install -y python36 python36-pip

您可以使用以下命令创建您的virtualenvpyvenv

python3.6 -m venv /tmp/foo

如果使用pyvenv脚本,则会收到警告:

$ pyvenv-3.6 /tmp/foo
WARNING: the pyenv script is deprecated in favour of `python3.6 -m venv`

解决方案2:IUS社区存储库

IUS社区提供了一些 有关RHEL和CentOS的最新软件包。后面的人来自Rackspace,所以我认为他们值得信赖…

https://ius.io/

在此处为您检查正确的仓库:

https://ius.io/setup

[IUS]如何在CentOS 6上安装Python 3.6

sudo yum install -y https://repo.ius.io/ius-release-el6.rpm
sudo yum install -y python36u python36u-pip

您可以使用以下命令创建您的virtualenvpyvenv

python3.6 -m venv /tmp/foo

[IUS]如何在CentOS 7上安装Python 3.6

sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
sudo yum install -y python36u python36u-pip

您可以使用以下命令创建您的virtualenvpyvenv

python3.6 -m venv /tmp/foo

Installing from RPM is generally better, because:

  • you can install and uninstall (properly) python3.
  • the installation time is way faster. If you work in a cloud environment with multiple VMs, compiling python3 on each VMs is not acceptable.

Solution 1: Red Hat & EPEL repositories

Red Hat has added through the EPEL repository:

  • Python 3.4 for CentOS 6
  • Python 3.6 for CentOS 7

[EPEL] How to install Python 3.4 on CentOS 6

sudo yum install -y epel-release
sudo yum install -y python34

# Install pip3
sudo yum install -y python34-setuptools  # install easy_install-3.4
sudo easy_install-3.4 pip

You can create your virtualenv using pyvenv:

pyvenv /tmp/foo

[EPEL] How to install Python 3.6 on CentOS 7

With CentOS7, pip3.6 is provided as a package :)

sudo yum install -y epel-release
sudo yum install -y python36 python36-pip

You can create your virtualenv using pyvenv:

python3.6 -m venv /tmp/foo

If you use the pyvenv script, you’ll get a WARNING:

$ pyvenv-3.6 /tmp/foo
WARNING: the pyenv script is deprecated in favour of `python3.6 -m venv`

Solution 2: IUS Community repositories

The IUS Community provides some up-to-date packages for RHEL & CentOS. The guys behind are from Rackspace, so I think that they are quite trustworthy…

https://ius.io/

Check the right repo for you here:

https://ius.io/setup

[IUS] How to install Python 3.6 on CentOS 6

sudo yum install -y https://repo.ius.io/ius-release-el6.rpm
sudo yum install -y python36u python36u-pip

You can create your virtualenv using pyvenv:

python3.6 -m venv /tmp/foo

[IUS] How to install Python 3.6 on CentOS 7

sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
sudo yum install -y python36u python36u-pip

You can create your virtualenv using pyvenv:

python3.6 -m venv /tmp/foo

回答 2

除了gecco的答案外,我还将步骤3更改为:

./configure

至:

./configure --prefix=/opt/python3

然后,在安装后,您还可以:

# ln -s /opt/python3/bin/python3 /usr/bin/python3

这是为了确保安装不会与yum安装的python冲突。

请参阅我在Internet上找到的说明:

http://www.hosting.com/support/linux/installing-python-3-on-centosredhat-5x-from-source

In addition to gecco’s answer I would change step 3 from:

./configure

to:

./configure --prefix=/opt/python3

Then after installation you could also:

# ln -s /opt/python3/bin/python3 /usr/bin/python3

It is to ensure that installation will not conflict with python installed with yum.

See explanation I have found on Internet:

http://www.hosting.com/support/linux/installing-python-3-on-centosredhat-5x-from-source


回答 3

与Python 2.7和3.3一起,Red Hat Software Collections现在包括Python 3.4-均可在RHEL 6和7上运行。

RHSCL 2.0文档位于https://access.redhat.com/documentation/zh-CN/Red_Hat_Software_Collections/

加上developerblog.redhat.com上的许多文章。

编辑

请按照以下说明在RHEL 6/7或CentOS 6/7上安装Python 3.4:

# 1. Install the Software Collections tools:
yum install scl-utils

# 2. Download a package with repository for your system.
#  (See the Yum Repositories on external link. For RHEL/CentOS 6:)
wget https://www.softwarecollections.org/en/scls/rhscl/rh-python34/epel-6-x86_64/download/rhscl-rh-python34-epel-6-x86_64.noarch.rpm
#  or for RHEL/CentOS 7
wget https://www.softwarecollections.org/en/scls/rhscl/rh-python34/epel-7-x86_64/download/rhscl-rh-python34-epel-7-x86_64.noarch.rpm

# 3. Install the repo package (on RHEL you will need to enable optional channel first):
yum install rhscl-rh-python34-*.noarch.rpm

# 4. Install the collection:
yum install rh-python34

# 5. Start using software collections:
scl enable rh-python34 bash

Along with Python 2.7 and 3.3, Red Hat Software Collections now includes Python 3.4 – all work on both RHEL 6 and 7.

RHSCL 2.0 docs are at https://access.redhat.com/documentation/en-US/Red_Hat_Software_Collections/

Plus lot of articles at developerblog.redhat.com.

edit

Follow these instructions to install Python 3.4 on RHEL 6/7 or CentOS 6/7:

# 1. Install the Software Collections tools:
yum install scl-utils

# 2. Download a package with repository for your system.
#  (See the Yum Repositories on external link. For RHEL/CentOS 6:)
wget https://www.softwarecollections.org/en/scls/rhscl/rh-python34/epel-6-x86_64/download/rhscl-rh-python34-epel-6-x86_64.noarch.rpm
#  or for RHEL/CentOS 7
wget https://www.softwarecollections.org/en/scls/rhscl/rh-python34/epel-7-x86_64/download/rhscl-rh-python34-epel-7-x86_64.noarch.rpm

# 3. Install the repo package (on RHEL you will need to enable optional channel first):
yum install rhscl-rh-python34-*.noarch.rpm

# 4. Install the collection:
yum install rh-python34

# 5. Start using software collections:
scl enable rh-python34 bash

回答 4

使用SCL存储库。

sudo sh -c 'wget -qO- http://people.redhat.com/bkabrda/scl_python33.repo >> /etc/yum.repos.d/scl.repo'
sudo yum install python33
scl enable python27

(每当您要使用python27而不是系统默认值时,都必须运行此最后一个命令。)

Use the SCL repos.

sudo sh -c 'wget -qO- http://people.redhat.com/bkabrda/scl_python33.repo >> /etc/yum.repos.d/scl.repo'
sudo yum install python33
scl enable python27

(This last command will have to be run each time you want to use python27 rather than the system default.)


回答 5

Python3最近作为Python34被添加到EPEL7中。

当前(正在进行)正在努力制定有关如何在EPEL7中打包Python3的打包准则。

参见https://bugzilla.redhat.com/show_bug.cgi?id=1219411
https://lists.fedoraproject.org/pipermail/python-devel/2015-July/000721.html

Python3 was recently added to EPEL7 as Python34.

There is ongoing (currently) effort to make packaging guidelines about how to package things for Python3 in EPEL7.

See https://bugzilla.redhat.com/show_bug.cgi?id=1219411
and https://lists.fedoraproject.org/pipermail/python-devel/2015-July/000721.html


回答 6

您可以从此处下载RHEL6 / CentOS6的源RPM和二进制RPM。

这是从最新的Fedora开发源rpm到RHEL6 / CentOS6的反向移植

You can download a source RPMs and binary RPMs for RHEL6 / CentOS6 from here

This is a backport from the newest Fedora development source rpm to RHEL6 / CentOS6


回答 7

我看到的所有答案都是要求从代码中编译python3或安装二进制RPM软件包。这是启用EPEL(企业Linux的额外软件包)然后使用yum安装python的另一个答案。RHEL 7.5(Maipo)的步骤

yum install wget y
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm ivh epel-*.rpm
yum install python36

另请参阅链接

I see all the answers as either asking to compile python3 from code or installing the binary RPM package. Here is another answer to enable EPEL (Extra Packages for Enterprise Linux) and then install python using yum. Steps for RHEL 7.5 (Maipo)

yum install wget –y
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-XX.noarch.rpm # Verify actual RPM name by browsing dir over browser
rpm –ivh epel-*.rpm
yum install python36

Also see link


回答 8

我在使用python 2.7时遇到了同样的问题。请按照以下步骤成功升级到3.6。您也可以尝试以下一种

  1. 升级版本为2.x之前查看

    python --version
    Python 2.7.5
  2. 使用以下命令将python升级到3.x版本-

    百胜安装python3x

    用所需的版本号替换x

    即用于安装python 3.6执行

    yum install python36
  3. 之后,如果您想将此Python设置为默认版本,则在bashrc文件中添加

    vi〜/ .bashrc

    alias python='python3.6'
  4. 执行bash命令以应用设置

    bash 
  5. 现在您可以看到以下版本

    python --version
    Python 3.6.3

I was having the same issue using the python 2.7. Follow the below steps to upgrade successfully to 3.6. You can also try this one-

  1. See before upgrading version is 2.x

    python --version
    Python 2.7.5
    
  2. Use below command to upgrade your python to 3.x version-

    yum install python3x

    replace x with the version number you want.

    i.e. for installing python 3.6 execute

    yum install python36
    
  3. After that if you want to set this python for your default version then in bashrc file add

    vi ~/.bashrc

    alias python='python3.6'
    
  4. execute bash command to apply the settings

    bash 
    
  5. Now you can see the version below

    python --version
    Python 3.6.3
    

回答 9

通过Software Collections使用Python 3.5的三个步骤:

sudo yum install centos-release-scl
sudo yum install rh-python35
scl enable rh-python35 bash

请注意,最后一个命令不需要sudo。现在我们可以看到python 3是当前shell的默认值:

python --version
Python 3.5.1

如果您希望将Python 2作为当前shell的默认设置,则只需跳过最后一个命令。

现在,假设您的Python 3脚本给您一个类似的错误/usr/bin/env: python3: No such file or directory。这是因为安装通常是通过不寻常的路径完成的:

/opt/rh/rh-python35/root/bin/python3

以上通常是符号链接。如果要在启动时python3自动$PATH为所有用户添加到,则一种添加方法是添加以下文件:

sudo vim /etc/profile.d/rh-python35.sh

会有这样的东西:

#!/bin/bash

PATH=$PATH:/opt/rh/rh-python35/root/bin/

现在,重新启动后,如果我们这样做

python3 --version

它应该工作。一个exceptions是自动生成的用户,例如没有外壳的Jenkins服务器中的“ jenkins”。在这种情况下,手动添加路径到$PATH脚本将是一种方法。

最后,如果您正在使用sudo pip3安装软件包,但是它告诉您找不到pip3,则可能是您在/ etc / sudoers中有一个secure_path。检查应确认这一点。要在运行命令时临时使用标准PATH,可以执行以下操作:sudo visudo

sudo env "PATH=$PATH" pip3 --version

有关更多详细信息,请参见此问题。

注意:Software Collections有一个更新的Python 3.6,但是由于我在尝试安装Pycurl时遇到了很多麻烦,因此目前不推荐使用。对于Python 3.5来说,这不是问题,因为我刚sudo yum install sclo-python35-python-pycurl做完了就可以了。

Three steps using Python 3.5 by Software Collections:

sudo yum install centos-release-scl
sudo yum install rh-python35
scl enable rh-python35 bash

Note that sudo is not needed for the last command. Now we can see that python 3 is the default for the current shell:

python --version
Python 3.5.1

Simply skip the last command if you’d rather have Python 2 as the default for the current shell.

Now let’s say that your Python 3 scripts give you an error like /usr/bin/env: python3: No such file or directory. That’s because the installation is usually done to an unusual path:

/opt/rh/rh-python35/root/bin/python3

The above would normally be a symlink. If you want python3 to be automatically added to the $PATH for all users on startup, one way to do this is adding a file like:

sudo vim /etc/profile.d/rh-python35.sh

Which would have something like:

#!/bin/bash

PATH=$PATH:/opt/rh/rh-python35/root/bin/

And now after a reboot, if we do

python3 --version

It should just work. One exception would be an auto-generated user like “jenkins” in a Jenkins server which doesn’t have a shell. In that case, manually adding the path to $PATH in scripts would be one way to go.

Finally, if you’re using sudo pip3 to install packages, but it tells you that pip3 cannot be found, it could be that you have a secure_path in /etc/sudoers. Checking with sudo visudo should confirm that. To temporarily use the standard PATH when running commands you can do, for example:

sudo env "PATH=$PATH" pip3 --version

See this question for more details.

NOTE: There is a newer Python 3.6 by Software Collections, but I wouldn’t recommend it at this time, because I had major headaches trying to install Pycurl. For Python 3.5 that isn’t an issue because I just did sudo yum install sclo-python35-python-pycurl which worked out of the box.


回答 10

如果您使用的是RHEL,并且希望使用Red Hat支持的Python,请使用Red Hat软件集合(RHSCL)。Red Hat不支持EPEL和IUS软件包。上面的许多答案也指向CentOS软件集合。虽然可以安装它们,但它们不是Red Hat支持的RHEL软件包。

另外,票数最高的答案也提供了不好的建议-在RHEL上,您不想更改/usr/bin/python/usr/bin/python2因为您可能会破坏yum和使用其他RHEL管理工具。看一下/bin/yum,它是一个以开头的Python脚本#!/usr/bin/python。如果您从源代码编译Python,请不要make install以root用户身份进行操作。那将覆盖/usr/bin/python。如果中断yum,则很难恢复系统。

欲了解更多信息,请参阅如何安装Python 3,画中画,VENV,virtualenv中,并pipenv在RHELdevelopers.redhat.com。它涵盖了从RHSCL安装和使用Python 3,使用Python虚拟环境以及使用软件集合以及在RHEL上使用Python的许多技巧。

简而言之,要通过Red Hat Software Collections安装Python 3.6:

$ su -
# subscription-manager repos --enable rhel-7-server-optional-rpms \
   --enable rhel-server-rhscl-7-rpms
# yum -y install @development
# yum -y install rh-python36

# yum -y install rh-python36-numpy \
   rh-python36-scipy \ 
   rh-python36-python-tools \
   rh-python36-python-six

要使用软件集合,您必须启用它:

scl enable rh-python36 bash

但是,如果要永久启用Python 3,可以将以下内容添加到〜/ .bashrc中,然后注销并重新登录。现在,Python 3永久存在。

# Add RHSCL Python 3 to my login environment
source scl_source enable rh-python36

注意:执行此操作后,python现在输入即可提供Python 3.6而不是Python 2.7。

有关更多信息,请参见上面的文章。

If you are on RHEL and want a Red Hat supported Python, use Red Hat Software collections (RHSCL). The EPEL and IUS packages are not supported by Red Hat. Also many of the answers above point to the CentOS software collections. While you can install those, they aren’t the Red Hat supported packages for RHEL.

Also, the top voted answer gives bad advice – On RHEL you do not want to change /usr/bin/python, /usr/bin/python2 because you will likely break yum and other RHEL admin tools. Take a look at /bin/yum, it is a Python script that starts with #!/usr/bin/python. If you compile Python from source, do not do a make install as root. That will overwrite /usr/bin/python. If you break yum it can be difficult to restore your system.

For more info, see How to install Python 3, pip, venv, virtualenv, and pipenv on RHEL on developers.redhat.com. It covers installing and using Python 3 from RHSCL, using Python Virtual Environments, and a number of tips for working with software collections and working with Python on RHEL.

In a nutshell, to install Python 3.6 via Red Hat Software Collections:

$ su -
# subscription-manager repos --enable rhel-7-server-optional-rpms \
   --enable rhel-server-rhscl-7-rpms
# yum -y install @development
# yum -y install rh-python36

# yum -y install rh-python36-numpy \
   rh-python36-scipy \ 
   rh-python36-python-tools \
   rh-python36-python-six

To use a software collection you have to enable it:

scl enable rh-python36 bash

However if you want Python 3 permanently enabled, you can add the following to your ~/.bashrc and then log out and back in again. Now Python 3 is permanently in your path.

# Add RHSCL Python 3 to my login environment
source scl_source enable rh-python36

Note: once you do that, typing python now gives you Python 3.6 instead of Python 2.7.

See the above article for all of this and a lot more detail.


回答 11

如果您需要正式的RHEL软件包,可以使用RHSCL(红帽软件集合)

更多细节:

您必须有权访问Red Hat Customer Portal才能阅读全文。

If you want official RHEL packages you can use RHSCL (Red Hat Software Collections)

More details:

You have to have access to Red Hat Customer Portal to read full articles.


回答 12

这是我按照以下步骤安装Python3的步骤:

yum install wget
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz  
sudo tar xvf Python-3.*   
cd Python-3.* 
sudo ./configure --prefix=/opt/python3    
sudo make   
sudo make install   
sudo ln -s /opt/python3/bin/python3 /usr/bin/python3

$ /usr/bin/python3    
Python 3.6.0

Here are the steps i followed to install Python3:

yum install wget
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz  
sudo tar xvf Python-3.*   
cd Python-3.* 
sudo ./configure --prefix=/opt/python3    
sudo make   
sudo make install   
sudo ln -s /opt/python3/bin/python3 /usr/bin/python3

$ /usr/bin/python3    
Python 3.6.0

回答 13

yum install python34.x86_64如果您已epel-release安装,则可以正常工作,此答案说明了如何操作,并且我确认它可以正常工作RHEL 7.3

$ cat /etc/*-release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.3 (Maipo)

$ type python3
python3 is hashed (/usr/bin/python3)

yum install python34.x86_64 works if you have epel-release installed, which this answer explains how to, and I confirmed it worked on RHEL 7.3

$ cat /etc/*-release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.3 (Maipo)

$ type python3
python3 is hashed (/usr/bin/python3)

回答 14

对于Amazon Linux上的RHEL,必须使用python3:

须藤百胜安装python34-devel

For RHEL on Amazon Linux, using python3 I had to do :

sudo yum install python34-devel


回答 15

当SCL不可用时,完全工作36(基于Joys输入)

yum install wget y
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm ivh epel-*.rpm
yum install python36

sudo yum install python34-setuptools
sudo mkdir /usr/local/lib/python3.6
sudo mkdir /usr/local/lib/python3.6/site-packages

sudo easy_install-3.6 pip

最后激活环境…

pyvenv-3.6 py3
source py3/bin/activate

然后python3

Full working 36 when SCL is not available (based on Joys input)

yum install wget –y
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm
rpm –ivh epel-*.rpm
yum install python36

sudo yum install python34-setuptools
sudo mkdir /usr/local/lib/python3.6
sudo mkdir /usr/local/lib/python3.6/site-packages

sudo easy_install-3.6 pip

Finally activate the environment…

pyvenv-3.6 py3
source py3/bin/activate

Then python3


回答 16

您可以安装miniconda(https://conda.io/miniconda.html)。这不仅仅是python 3.7,但安装非常简单明了。

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O
sudo yum install bzip2
bash Miniconda3-latest-Linux-x86_64.sh

您必须接受许可协议,并在交互模式下选择一些选项(接受默认值)。我相信它也可以以某种方式静默安装。

You can install miniconda (https://conda.io/miniconda.html). That’s a bit more than just python 3.7 but the installation is very straightforward and simple.

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O
sudo yum install bzip2
bash Miniconda3-latest-Linux-x86_64.sh

You’ll have to accept the license agreement and choose some options in interactive mode (accept the defaults). I believe it can be also installed silently somehow.


回答 17

对于使用AWS EC2 RHEL 7.5的用户,(使用sudo)启用必需的存储库

yum-config-manager --enable rhui-REGION-rhel-server-optional
yum-config-manager --enable rhui-REGION-rhel-server-rhscl

安装Python 3.6

yum install rh-python36

安装其他依赖项

yum install rh-python36-numpy  rh-python36-scipy  rh-python36-python-tools  rh-python36-python-six

For those working on AWS EC2 RHEL 7.5, (use sudo) enable required repos

yum-config-manager --enable rhui-REGION-rhel-server-optional
yum-config-manager --enable rhui-REGION-rhel-server-rhscl

Install Python 3.6

yum install rh-python36

Install other dependencies

yum install rh-python36-numpy  rh-python36-scipy  rh-python36-python-tools  rh-python36-python-six