升級Python
查看系統(tǒng)版本
cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
查看Python版本
python -V
Python 2.7.5
安裝Python3
安裝所有的開發(fā)工具包
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查看編譯參數(shù)
# 默認(rèn)安裝在'/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 <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
# 設(shè)置版本
pyenv global 3.6.3
pyenv local 3.6.3
pyenv shell 3.6.3
pyenv shell --unset
# 說明
# global 設(shè)置全局的Python版本,通過將版本號寫入 ~/.pyenv/version 文件的方式
# local 設(shè)置面向程序的本地版本,通過將版本號寫入當(dāng)前目錄下的 .python-version 文件的方式pyenv
# shell 設(shè)置面向 shell 的 Python 版本,通過設(shè)置當(dāng)前 shell 的 PYENV_VERSION 環(huán)境變量的方式。
# --unset 參數(shù)可以用于取消當(dāng)前 shell 設(shè)定的版本。
管理python版本
# 查看幫助
pyenv help install
# 查看通過pyenv可安裝的python版本
pyenv install -l
# 安裝指定版本,-v顯示安裝細(xì)節(jié)
pyenv install -v 2.7.14
pyenv install -v 3.6.3
# 卸載一個版本
pyenv uninstall 2.7.14
# 每次安裝或卸載一個版本時都要執(zhí)行如下命令
# 為所有已安裝的可執(zhí)行文件(如:`~/.pyenv/versions/*/bin/*`)創(chuàng)建shims
pyenv rehash
使用鏡像
鏡像:
手動安裝
下載需要的版本放到~/.pyenv/cache文件夾下面
# 修改下載鏈接
vi /root/.pyenv/plugins/python-build/share/python-build/2.7.6
然后執(zhí)行 pyenv install 版本號 安裝對應(yīng)的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
設(shè)置鏡像變量
# 設(shè)置鏡像URL變量
export PYTHON_BUILD_MIRROR_URL="http://pyenv.qiniudn.com/pythons/"
# 安裝2.7.5
pyenv install 2.7.5 -v
搭鏡像服務(wù)
重命名安裝包成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))
設(shè)置鏡像地址
export PYTHON_BUILD_MIRROR_URL="http://127.0.0.1:8000/"
# or
export PYTHON_BUILD_MIRROR_URL="http://0.0.0.0:8000/"
開啟服務(wù)
# 一定要切換到包含鏡像的目錄下執(zhí)行如下命令
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] <virtualenv-name>
pyenv virtualenv --version
pyenv virtualenv --help
-f/--force Install even if the version appears to be installed already
有了pyenv-virtualenv以后,我們可以為同一個Python解釋器,創(chuàng)建多個不同的"工作環(huán)境"。
# 例如,我們新建兩個工作環(huán)境:
pyenv virtualenv 2.7.14 first_project
pyenv virtualenv 2.7.14 second_project
# 可以使用virtualenvs子命令查看工作環(huán)境
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子命令進(jìn)入或退出一個工作環(huán)境
pyenv activate first_project
# 如果想要刪除虛擬環(huán)境,則使用:
pyenv virtualenv-delete first_project
管理Python包
setuptools
# easy_install命令被安裝在/usr/local/bin目錄下
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
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/
- 中國科技大學(xué) https://pypi.mirrors.ustc.edu.cn/simple/
- 華中理工大學(xué):http://pypi.hustunique.com/
- 山東理工大學(xué):http://pypi.sdutlinux.org/