目前大部分用戶使用的CentOS6.5上默認的Python仍是2.6版本。升級到Python2.7碰到不少問題。本文將介紹如何安裝Python2.7。python
1. 安裝必要的準備包sql
安裝過程將用到gcc,方便起見,安裝「Development Tools」app
yum groupinstall "Development tools"
另外,Python安裝中須要的一些依賴包less
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel
2. 下載安裝Python2.7socket
目前Python2.7的最新版本是2.7.11。能夠在下面的網站查詢:工具
https://www.python.org/ftp/python/網站
2.7.11的下載連接是:ui
https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgzthis
wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
tar vxf Python-2.7.11.tgz
cd Python-2.7.11.tgz
./configure --prefix=/usr/local
make && make install
安裝完成後,經過運行python,能夠看到版本spa
>>> import sys >>> sys.version '2.7.11 (default, May 6 2016, 01:38:00) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)]'
3.安裝pip
pip是python的安裝工具,不少python的經常使用工具,均可以經過pip進行安裝。
要安裝pip,首先要安裝setuptools。下面的連接能夠獲得相關信息,最新版本是21.0.0:
https://pypi.python.org/pypi/setuptools
下載連接:
https://pypi.python.org/packages/ff/d4/209f4939c49e31f5524fa0027bf1c8ec3107abaf7c61fdaad704a648c281/setuptools-21.0.0.tar.gz#md5=81964fdb89534118707742e6d1a1ddb4
一樣的,進行安裝:
tar vxf setuptools-21.0.0.tar.gz cd setuptools-21.0.0 python setup.py install
安裝完成後,下載pip。其信息在以下網站:
https://pypi.python.org/pypi/pip
最新版是8.1.1,下載連接:
https://pypi.python.org/packages/41/27/9a8d24e1b55bd8c85e4d022da2922cb206f183e2d18fee4e320c9547e751/pip-8.1.1.tar.gz#md5=6b86f11841e89c8241d689956ba99ed7
一樣的,進行安裝
tar vxf pip-8.1.1.tar.gz cd pip-8.1.1 python setup.py install
安裝完成後,運行pip
[root@hwthstest08 pip-8.1.1]# pip Usage: pip <command> [options] Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. search Search PyPI for packages. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion help Show help for commands. General Options: -h, --help Show help. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. --log <path> Path to a verbose appending log. --proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port. --retries <retries> Maximum number of retries each connection should attempt (default 5 times). --timeout <sec> Set the socket timeout (default 15 seconds). --exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup. --trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS. --cert <path> Path to alternate CA bundle. --client-cert <path> Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. --cache-dir <dir> Store the cache data in <dir>. --no-cache-dir Disable the cache. --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index. [root@hwthstest08 pip-8.1.1]#
安裝一個程序檢測:
[root@hwthstest08 pip-8.1.1]# pip install psutil Collecting psutil Downloading psutil-4.1.0.tar.gz (301kB) 100% |████████████████████████████████| 307kB 274kB/s Installing collected packages: psutil Running setup.py install for psutil ... done Successfully installed psutil-4.1.0
4. 載入經常使用的tab.py程序
#!/usr/bin/evn python import sys import readline import rlcompleter import atexit import os readline.parse_and_bind('tab: complete') histfile = os.path.join(os.environ['HOME'], '.pythonhistory') try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter