CentOS 6.7默认的Python版本为2.6.6,现升级为Python 2.7.11

1、安装编译环境

yum groupinstall "Development tools"

2、安装依赖的包

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

3、下载Python

wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tgz
tar zxvf Python-2.7.11.tgz
cd Python-2.7.11

4、编译安装

./configure --prefix=/usr/local/python
make && make install

5、直接调用

/usr/local/python/bin/python

6、替换旧版本

  ln -sf /usr/local/python/bin/python /usr/bin/python

可以把python目录下的bin目录加入系统环境变量:

   echo 'export PATH=$PATH:/usr/local/python/bin' > /etc/profile.d/py.sh

   sh /etc/profile.d/py.sh

重新会话登录后生效。

7、查看版本

python -V
Python 2.7.11

8、升级后yum出错的解决方法

使用该方法升级Python可能会导致yum对python的依赖版本不对,可以修改/usr/bin/yum文件

首行改为:

#!/usr/bin/python2.6


9、其他用到的:

1)安装pip:

    wget https://bootstrap.pypa.io/get-pip.py 

    python get-pip.py

2)设置python命令行补全功能:

    pip install readline

  1. cat ~/.pystartup

  2. # Add auto-completion and a stored history file of commands to your Python

    # interactive interpreter. Requires Python 2.0+, readline. Autocomplete is

    # bound to the Esc key by default (you can change it - see readline docs).

    #

    # Store the file in ~/.pystartup, and set an environment variable to point

    # to it:  "export PYTHONSTARTUP=~/.pystartup" in bash.

  3. import atexit

    import os

    import readline

    import rlcompleter

  4. readline.parse_and_bind('tab: complete')

  5. historyPath = os.path.expanduser("~/.pyhistory")

  6. def save_history(historyPath=historyPath):

        import readline

        readline.write_history_file(historyPath)

  7. if os.path.exists(historyPath):

        readline.read_history_file(historyPath)

  8. atexit.register(save_history)

    del os, atexit, readline, rlcompleter, save_history, historyPath

立即生效:

  1. export PYTHONSTARTUP=~/.pystartup  

重启生效:

  1. echo "export PYTHONSTARTUP=~/.pystartup" >> /etc/profile.d/py.sh


3)安装Ipython更加直接方便,安装命令:

   pip install ipython

   使用:

   ipython