CentOS下Python管理

一、升级Python

查看系统版本

cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

查看Python版本

python -V
Python 2.7.5

1.1 安装Python3

安装所有的开发工具包

yum groupinstall "Development tools" -y
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel -y

下载最新的python安装包,从下面链接中找到要下载Python版本的源码包,国内的比较快

# 下载
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
wget http://mirrors.sohu.com/python/3.6.5/Python-3.6.5.tgz

# 解压
tar -xavf Python-3.6.5.tgz
cd Python-3.6.5

# 编译安装
# ./configure --help查看编译参数
# 默认安装在'/usr/local/bin','/usr/local/lib' etc
./configure && make && make install

# 安装后
python -V
python3 -V
which python
which python3
whereis python
whereis python3

二、setuptools和pip

2.1 setuptools

当需要安装第三方python包时,可能会用到easy_install命令。easy_install是由PEAK(Python Enterprise Application Kit)开发的setuptools包里带的一个命令,所以使用easy_install实际上是在调用setuptools来完成安装模块的工作。

通过引导程序 ez_setup.py 来安装

# easy_install命令被安装在/usr/local/bin目录下
# http://peak.telecommunity.com/dist/ez_setup.py
# https://bootstrap.pypa.io/ez_setup.py
wget https://bootstrap.pypa.io/ez_setup.py -O - | python

下载setuptools的egg包,然后通过sh安装

setuptools-0.6c11-py2.7.egg

sh setuptools-0.6c11-py2.7.egg

编译安装

下载 https://pypi.org/project/setuptools/#files

# 下载whl
wget https://files.pythonhosted.org/packages/ff/f4/385715ccc461885f3cedf57a41ae3c12b5fec3f35cce4c8706b1a112a133/setuptools-40.0.0-py2.py3-none-any.whl
pip install setuptools-40.0.0-py2.py3-none-any.whl

# 下载zip,源码编译安装
wget https://files.pythonhosted.org/packages/d3/3e/1d74cdcb393b68ab9ee18d78c11ae6df8447099f55fe86ee842f9c5b166c/setuptools-40.0.0.zip
unzip setuptools-40.0.0.zip
cd setuptools-40.0.0
python setup.py install

使用

# pip命令被安装在/usr/local/bin目录下了
easy_install pip

2.2 pip

安装pip

# 使用easy_install
easy_install pip

# https://pypi.python.org/pypi/pip
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz
tar -xzvf pip-9.0.1.tar.gz && cd pip-9.0.1
python setup.py install

# https://pip.pypa.io/en/stable/installing/
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

配置镜像加速

如何使用科大镜像加速pip https://lug.ustc.edu.cn/wiki/mirrors/help/pypi

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider

配置镜像 ~/.pip/pip.conf

[global]
index-url = https://mirrors.ustc.edu.cn/pypi/web/simple
format = columns

三、pyenv管理Python版本

3.1 安装

https://github.com/pyenv/pyenv#installation

# 1.Check out pyenv where you want it installed.
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# 2.Define environment variable PYENV_ROOT
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

# 3.Add pyenv init to your shell 
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi' >> ~/.bash_profile
 
# 4.Restart your shell so the path changes take effect.
# exec "$SHELL"
source ~/.bash_profile

# 5.Install Python versions into $(pyenv root)/versions.
pyenv install 2.7.8

升级

# 升级
cd $(pyenv root)
git pull

# 切换版本分支
cd $(pyenv root)
git fetch
git tag
git checkout v0.1.0

卸载

rm -rf $(pyenv root)

3.2 使用

命令

# pyenv共11条命令
[root@www ~]# pyenv 
pyenv 1.1.5
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable
   
See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

查看python版本

# 查看版本
pyenv versions
pyenv version

# 设置版本
pyenv global 3.6.3
pyenv local 3.6.3
pyenv shell 3.6.3
pyenv shell --unset

# 说明
# global 设置全局的Python版本,通过将版本号写入 ~/.pyenv/version 文件的方式
# local 设置面向程序的本地版本,通过将版本号写入当前目录下的 .python-version 文件的方式pyenv
# shell 设置面向 shell 的 Python 版本,通过设置当前 shell 的 PYENV_VERSION 环境变量的方式。
# --unset 参数可以用于取消当前 shell 设定的版本。

管理python版本

# 查看帮助
pyenv help install

# 查看通过pyenv可安装的python版本
pyenv install -l

# 安装指定版本,-v显示安装细节
pyenv install -v 2.7.14
pyenv install -v 3.6.3

# 卸载一个版本
pyenv uninstall 2.7.14

# 每次安装或卸载一个版本时都要执行如下命令
# 为所有已安装的可执行文件(如:`~/.pyenv/versions/*/bin/*`)创建shims
pyenv rehash

3.3 使用镜像

镜像:

手动安装

下载需要的版本放到~/.pyenv/cache文件夹下面

# 修改下载链接
vi /root/.pyenv/plugins/python-build/share/python-build/2.7.6

然后执行 pyenv install 版本号 安装对应的python版本
pyenv install 3.6.3 -v

一键脚本安装

# pyenv install 3.6.3
v=3.6.3|wget http://mirrors.sohu.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/;pyenv install $v -v

# 分步安装
v=3.6.3
wget http://mirrors.sohu.com/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/
pyenv install $v -v

设置镜像变量

# 设置镜像URL变量
export PYTHON_BUILD_MIRROR_URL="http://pyenv.qiniudn.com/pythons/"

# 安装2.7.5
pyenv install 2.7.5 -v

3.4 搭镜像服务

重命名安装包成64位sha码

sha.py

# -*- coding:utf-8 -*-
import os
import hashlib
import sys
__author__ = 'dave'
def get_hash(filepath):
    if not os.path.exists(filepath):
        print('File not exists.')
        return
    # algo = hashlib.md5()
    algo = hashlib.sha256()
    with open(filepath, 'rb') as f:
        while True:
            data = f.read(4096)
            if not data:
                break
            algo.update(data)
    return algo.hexdigest()
if __name__ == '__main__':
    filepath = sys.argv[1]
    # md5sum = get_hash('Python-3.3.6.tar.xz')
    md5sum = get_hash(filepath)
    print(md5sum)
    print(len(md5sum))

设置镜像地址

export PYTHON_BUILD_MIRROR_URL="http://127.0.0.1:8000/"
# or
export PYTHON_BUILD_MIRROR_URL="http://0.0.0.0:8000/"

开启服务

# 一定要切换到包含镜像的目录下执行如下命令
cd ~/.pyenv/cache/

# python3
python -m http.server
# python2
python -m SimpleHTTPServer

安装

# 再打开一个终端窗口
pyenv install 3.3.6

四、virtualenv管理Python项目

4.1 安装

https://github.com/pyenv/pyenv-virtualenv

# 1.Check out pyenv-virtualenv into plugin directory
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv

# 2.Add pyenv virtualenv-init to your shell
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
# Fish shell note: Add this to your ~/.config/fish/config.fish
# status --is-interactive; and source (pyenv virtualenv-init -|psub)

# 3.Restart your shell to enable pyenv-virtualenv
# exec "$SHELL"
source ~/.bash_profile

4.2 使用

查看帮助

pyenv help virtualenv
Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] <virtualenv-name>
       pyenv virtualenv --version
       pyenv virtualenv --help

  -f/--force       Install even if the version appears to be installed already

有了pyenv-virtualenv以后,我们可以为同一个Python解释器,创建多个不同的"工作环境"。

# 例如,我们新建两个工作环境:
pyenv virtualenv 2.7.14 first_project
pyenv virtualenv 2.7.14 second_project

# 可以使用virtualenvs子命令查看工作环境
pyenv virtualenvs
  2.7.14/envs/first_project (created from /root/.pyenv/versions/2.7.14)
  2.7.14/envs/second_project (created from /root/.pyenv/versions/2.7.14)
  first_project (created from /root/.pyenv/versions/2.7.14)
  second_project (created from /root/.pyenv/versions/2.7.14)

# 通过activate和deactivate子命令进入或退出一个工作环境
pyenv activate first_project

# 如果想要删除虚拟环境,则使用:
pyenv virtualenv-delete first_project

五、插件镜像

原文地址:https://www.cnblogs.com/okokabcd/p/8564354.html