升级python而不破坏yum
问题描述
我最近通过从源代码编译在 CentOS 机器上安装了 Python 2.7.3.Python 2.7.3 安装在/opt/python2.7 中,当我安装它时,我只是将/usr/bin/python 更改为指向新版本.这显然是错误的,因为当我这样做时它打破了百胜.我会得到以下内容.
I recently installed Python 2.7.3 on a CentOS machine by compiling from source. Python 2.7.3 is installed at /opt/python2.7 and when I installed it I just changed /usr/bin/python to point to the new version. This apparently is wrong though because when I did it it broke yum. I would get the following.
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
No module named yum
Please install a package which provides this module, or
verify that the module is installed correctly.
It's possible that the above module doesn't match the
current version of Python, which is:
2.7.3 (default, May 15 2012, 17:45:42)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)]
If you cannot solve this problem yourself, please go to
the yum faq at:
http://yum.baseurl.org/wiki/Faq
我将/usr/bin/python 更改为指向 python 2.6.6,但现在 2.6.6 是 python 的默认版本.知道如何解决这个问题吗?
I changed /usr/bin/python to point back to the python 2.6.6 but now 2.6.6 is the default version of python. Any idea how to fix this?
解决方案
我写了一个快速指南关于如何在 CentOS 6 和 CentOS 7 上安装最新版本的 Python 2 和 Python 3.它目前涵盖 Python 2.7.13 和 Python 3.6.0:
I have written a quick guide on how to install the latest versions of Python 2 and Python 3 on CentOS 6 and CentOS 7. It currently covers Python 2.7.13 and Python 3.6.0:
# Start by making sure your system is up-to-date:
yum update
# Compilers and related tools:
yum groupinstall -y "development tools"
# Libraries needed during compilation to enable all features of Python:
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel
# If you are on a clean "minimal" install of CentOS you also need the wget tool:
yum install -y wget
接下来的步骤取决于您安装的 Python 版本.
The next steps depend on the version of Python you're installing.
对于 Python 2.7.14:
For Python 2.7.14:
wget http://python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
tar xf Python-2.7.14.tar.xz
cd Python-2.7.14
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
# Strip the Python 2.7 binary:
strip /usr/local/lib/libpython2.7.so.1.0
对于 Python 3.6.3:
For Python 3.6.3:
wget http://python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
tar xf Python-3.6.3.tar.xz
cd Python-3.6.3
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
make && make altinstall
# Strip the Python 3.6 binary:
strip /usr/local/lib/libpython3.6m.so.1.0
要安装 Pip:
# First get the script:
wget https://bootstrap.pypa.io/get-pip.py
# Then execute it using Python 2.7 and/or Python 3.6:
python2.7 get-pip.py
python3.6 get-pip.py
# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]
您不应该更改 Python 的系统版本,因为它会破坏系统(正如您所发现的).只要您不理会原始系统版本,安装其他版本就可以正常工作.这可以通过在运行 configure 时使用自定义前缀(例如 /usr/local
)和使用 make altinstall
(而不是普通的 make install
) 安装您的 Python 版本时.
You are not supposed to change the system version of Python because it will break the system (as you found out). Installing other versions works fine as long as you leave the original system version alone. This can be accomplished by using a custom prefix (for example /usr/local
) when running configure, and using make altinstall
(instead of the normal make install
) when installing your build of Python.
只要您记得输入包含版本号的全名(例如python2.7"或pip2.7"),拥有多个可用的 Python 版本通常不是什么大问题.如果您从 virtualenv 完成所有 Python 工作,则版本控制会为您处理,因此请确保您安装并使用 virtualenv!
Having multiple versions of Python available is usually not a big problem as long as you remember to type the full name including the version number (for example "python2.7" or "pip2.7"). If you do all your Python work from a virtualenv the versioning is handled for you, so make sure you install and use virtualenv!
相关文章