1.在CentOS下安装Python

来源:互联网 发布:棋逢对手 知乎 编辑:程序博客网 时间:2024/06/03 04:31

(本文内容摘抄自Panda老师的笔记)

1.1安装python

A:软件包安装——python的安装配置 (以centos6为例,自带Python为2.6)

yum install openssl-devel openss python-devel zlib*  wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgztar zxvf Python-2.7.5.tgzcd Python-2.7.5./configure --prefix=/usr/local/python27  makemake install

B:Python环境变量配置

cd /usr/binmv python python26  # 将老版本的Python执行文件重命名,由于yum依赖的是老版本,不能删除ln -s /usr/local/python27/bin/python   /usr/bin/python   # 将新版本Python做软连接或者拷贝which python    # 显示当前python执行文件的地址           python -V            # 显示当前使用的版本信息whereis python   # 显示所有不同版本的所有python信息

C:yum文件的修改

cat /usr/bin/yum#!/usr/bin/python26     # 修改为原来python版本

D: 上面的方法直接修改了整个系统的python可能会影响到别人的环境,下面是更优雅的方式

mkdir -p  $HOME/apps/python27tar zxvf  Python-2.7.5.tgzcd Python-2.7.5./configure --prefix=$HOME/apps/python27 --with-zlib-dir=/usr/local/libmake && make install $ cat ~/.bashrc       # 将家目录的python加入到环境变量优先加载export PATH=$HOME/apps/python27/bin:$PATH$ source ~/.bashrc $ which python       # 成功~/apps/python27/bin/python

1.2. python的包管理——pip

https://pypi.python.org/pypi/pip/ 官网
https://pip.pypa.io/en/stable/installing/ 官方文档

wget https://bootstrap.pypa.io/get-pip.py   官网直接下载python get-pip.py pip install -U pip        # 更新pip uninstall pip       # 卸载pip install ipython   # 安装需要的包pip install "django>1.8,<1.9"pip search  关键词    # 搜索包pip freeze > test.txt    # 讲pip安装的包及版本号写入文件pip install -r test.txt    # 安装文件中的包

备注:
python get-pip.py 报错如下:
zipimport.ZipImportError: can’t decompress data; zlib not available
解决方案
./configure –prefix=/usr/local/python27 –with-zlib-dir=/usr/local/lib
在国内的强烈推荐豆瓣的源,注意后面要有/simple目录。
http://pypi.douban.com/simple/
使用镜像源很简单,用-i指定就行了:

配置当前用户的python源

# vim  ~/.pip/pip.conf      [global]trusted-host=pypi.douban.comindex-url = http://pypi.douban.com/simple[list]             # pip list的格式更优雅format=columns

配置全局用户的pip源码环境:

# cat /etc/pip.conf [global] trusted-host = pypi.douban.comindex-url = http://pypi.douban.com/simple [list] format=columns