CentOS下Python管理

升级Python

查看系统版本

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

查看Python版本

python -V
Python 2.7.5

安装Python3

安装所有的开发工具包

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

下载最新的python安装包

https://www.python.org/downloads/

# 下载
# wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
wget http://mirrors.sohu.com/python/3.6.4/Python-3.6.4.tgz
# 解压
tar -xavf Python-2.7.14.tgx
cd Python-2.7.14
# 编译安装
# ./configure --help查看编译参数
# 默认安装在'/usr/local/bin','/usr/local/lib' etc
./configure && make && make install

pyenv管理Python版本

安装

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\n  eval "$(pyenv init -)"\nfi' >> ~/.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)

使用

命令

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

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 ' 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

使用镜像

镜像:

  • 镜像:http://mirrors.sohu.com/python/

手动安装

下载需要的版本放到~/.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

搭镜像服务

重命名安装包成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项目

安装

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

使用

查看帮助

pyenv help virtualenv
Usage: pyenv virtualenv [-f|--force] [VIRTUALENV_OPTIONS] [version] 
       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

管理Python包

setuptools

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

setuptools-0.6c11-py2.7.egg

sh setuptools-0.6c11-py2.7.egg

安装pip

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

pip

如何使用科大镜像加速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

插件镜像

  • 七牛镜像:http://pyenv.qiniudn.com/pythons/
  • 科大镜像 https://mirrors.ustc.edu.cn/pypi/web/simple
  • 豆瓣镜像 https://pypi.douban.com/simple/
  • 官方插件 https://pypi.python.org/pypi
  • PyPI Official Mirrors: https://pypi.python.org/mirrors
  • PEP-381 Mirroring Protocol: http://www.python.org/dev/peps/pep-0381/
  • bandersnatch: https://pypi.python.org/pypi/bandersnatch
  • 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  • 阿里云:http://mirrors.aliyun.com/pypi/simple/
  • 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  • 华中理工大学:http://pypi.hustunique.com/
  • 山东理工大学:http://pypi.sdutlinux.org/

你可能感兴趣的:(CentOS下Python管理)