一、升级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\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)

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

五、插件镜像

CentOS下Python管理的更多相关文章

  1. Linux CentOS下Python+robot framework环境搭建

    Linux CentOS下Python+robot framework环境搭建   by:授客 QQ:1033553122 操作系统环境:CentOS 6.5-x86_64 下载地址:http://w ...

  2. Centos下磁盘管理的常用命令记录(如查找大文件)

    Centos下磁盘管理的常用命令记录 查看系统磁盘空间占用,使用命令: df -h 结果: 查看磁盘inode使用情况,如果inode用完了,磁盘就没法写入新的内容了: df -i 结果: 如何查找磁 ...

  3. centos 下Python独立虚拟环境创建

    virtualenv Python有着庞大的开源社区的支持,很自然就产生这么一个问题:第三方包参差不齐,如果我们想在服务器测试,或者升级某个包,就会导致生产环境产生杂乱,多余的第三方依赖包. virt ...

  4. centos下python多版本管理(pyenv+python+virtualenv+ipython)

    pyenv是个多版本python管理器,可以同时管理多个python版本共存,如pypy,miniconde等等 1 环境准备 安装相关软件和pyenv1.1 安装相关软件yum install -y ...

  5. centos下python安装与虚拟环境配置

    Centos7下安装Python3.7 首先安装依赖包,centos里面是-devel,如果在ubuntu下安装则要改成-dev,依赖包缺一不可,笔者曾安装python3未成功就是因为没有安装libf ...

  6. Centos下软件包管理

    目录 一.安装软件包的三种方法 二.rpm包介绍 三.rpm工具用法 四.yum工具用法 五.yum搭建本地仓库 六.yum更换国内源 七.yum下载rpm包 八.源码包安装 九.扩展 一.安装软件包 ...

  7. Linux CentOS下Python+robot framework环境搭建

    转载自:http://blog.sina.com.cn/s/blog_13cc013b50102vof1.html 操作系统环境:CentOS 6.5-x86_64 下载地址:http://www.c ...

  8. centos下python中添加easygui模块

    前提:python中要集成Tkinter,Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同 ...

  9. centos下python的pymssql模块安装及简单使用

    1.安装pymssq模块 1-1.环境准备: 1-1-1.unixODBC安装 yum install unixODBC unixODBC-devel -y 1-1-2.freetds安装 下载 fr ...

随机推荐

  1. 阿里云、腾讯云、CentOS下的MySQL的安装与配置详解

    一. 安装 查看是否已安装 # 查看MySQL版本 mysql --version # 查看MySQL相关文件 whereis mysql 若已安装,卸载方法如下 # 卸载MySQL yum remo ...

  2. 技术架构:IT生存之道

    Technical architecture: What IT does for a living (cio.com) Technical architecture: What IT does for ...

  3. logback日志级别动态切换的终极方案(asm使用)

    背景 一切皆有因果,所有事情,都有事件驱动.本方案的日志级别切换是由这样的背景下产生的: 单个生产环境上,有几百近千个微服务 日志级别切换不重启服务,要求即时生效果 由业务开发人员去修改代码或增加相关 ...

  4. PHP截取字符串(指定开始和结束的字符串)

  5. 基于HTML5的网络拓扑图(1)

    什么是网络拓扑 网络拓扑,指构成网络的成员间特定的排列方式.分为物理的,即真实的.或者逻辑的,即虚拟的两种.如果两个网络的连接结构相同,我们就説它们的网络拓扑相同,尽管它们各自内部的物理接线.节点间距 ...

  6. Java/C++实现装饰模式---模拟手机功能的升级过程

    用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人:而JarPhone除了声音还能振动:更高级的手机(ComplexPhone)除了声音.振动外,还 ...

  7. This program may be freely redistributed under the terms of the GNU GPL

    在centos中安装Google浏览器时 执行[root@server1 opt]# rpm ivh install google-chrome-stable_current_x86_64.rpm 爆 ...

  8. Android修改app图标

    1.按照路径找到AndroidManifest.xml中的icon 2.在drawable添加一个png图片 3.然后在AndroidManifest.xml中的icon,修改其中的值 android ...

  9. vue-cli打包后dist文件运行空白和背景图显示问题详解

    1.文件引用路径.我们直接运行打包后的文件夹中的index.html文件,会看到网页一片空白,f12调试,全是css,js路径引用错误的问题. 解决:到config文件夹中打开index.js文件. ...

  10. LazyCaptcha自定义随机验证码和字体

    介绍 LazyCaptcha是仿EasyCaptcha和SimpleCaptcha,基于.Net Standard 2.1的图形验证码模块. 目前Gitee 52star, 如果对您有帮助,请不吝啬点 ...