pyenv可以帮助开发者安装和管理多个Python版本,创建虚拟环境等。
Git地址:https://github.com/pyenv/pyenv
安装说明:https://github.com/pyenv/pyenv-installer
0 预安装条件
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl
更改 libreadline-dev环境:
sudo apt install libedit-dev
1 安装、更新、删除pyenv
1.1 安装pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 148 100 148 0 0 251 0 --:--:-- --:--:-- --:--:-- 251
100 2188 100 2188 0 0 1146 0 0:00:01 0:00:01 --:--:-- 1878
正克隆到 '/home/zhou/.pyenv'...
remote: Enumerating objects: 616, done.
remote: Counting objects: 100% (616/616), done.
remote: Compressing objects: 100% (428/428), done.
remote: Total 616 (delta 297), reused 273 (delta 98), pack-reused 0
接收对象中: 100% (616/616), 359.06 KiB | 156.00 KiB/s, 完成.
处理 delta 中: 100% (297/297), 完成.
正克隆到 '/home/zhou/.pyenv/plugins/pyenv-doctor'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 11 (delta 1), reused 6 (delta 1), pack-reused 0
展开对象中: 100% (11/11), 完成.
正克隆到 '/home/zhou/.pyenv/plugins/pyenv-installer'...
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 17 (delta 2), reused 9 (delta 1), pack-reused 0
展开对象中: 100% (17/17), 完成.
正克隆到 '/home/zhou/.pyenv/plugins/pyenv-update'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
展开对象中: 100% (10/10), 完成.
正克隆到 '/home/zhou/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 57, done.
remote: Counting objects: 100% (57/57), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 57 (delta 10), reused 19 (delta 0), pack-reused 0
展开对象中: 100% (57/57), 完成.
正克隆到 '/home/zhou/.pyenv/plugins/pyenv-which-ext'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 1), reused 6 (delta 0), pack-reused 0
展开对象中: 100% (10/10), 完成.
WARNING: seems you still have not added 'pyenv' to the load path.
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/home/zhou/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
在.bashrc文件中手动添加pyenv环境变量路径:
export PATH="/home/zhou/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
使.bashrc文件生效:
$ source .bashrc
1.2 更新pyenv
$ pyenv update
Updating /home/zhou/.pyenv...
来自 https://github.com/pyenv/pyenv
* branch master -> FETCH_HEAD
已经是最新的。
Updating /home/zhou/.pyenv/plugins/pyenv-doctor...
来自 https://github.com/pyenv/pyenv-doctor
* branch master -> FETCH_HEAD
已经是最新的。
Updating /home/zhou/.pyenv/plugins/pyenv-installer...
来自 https://github.com/pyenv/pyenv-installer
* branch master -> FETCH_HEAD
已经是最新的。
Updating /home/zhou/.pyenv/plugins/pyenv-update...
来自 https://github.com/pyenv/pyenv-update
* branch master -> FETCH_HEAD
已经是最新的。
Updating /home/zhou/.pyenv/plugins/pyenv-virtualenv...
来自 https://github.com/pyenv/pyenv-virtualenv
* branch master -> FETCH_HEAD
已经是最新的。
Updating /home/zhou/.pyenv/plugins/pyenv-which-ext...
来自 https://github.com/pyenv/pyenv-which-ext
* branch master -> FETCH_HEAD
已经是最新的。
1.3 删除pyenv
rm -fr ~/.pyenv
同时删除 .bashrc文件中pyenv环境变量内容
export PATH="/home/zhou/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
2 pyenv 安装相关软件
pyenv安装源位于国外,因此在安装python相关软件时非常慢。
查看pyenv可安装的软件:
$ pyenv install --list
pyenv可安装Python相关软件设置位于:~/.pyenv/plugins/python-build/share/python-build,打开其中anaconda3-5.3.1文件可以看到其中的安装文件源地址:
case "$(anaconda_architecture 2>/dev/null || true)" in
2 "Linux-x86" )
3 install_script "Anaconda3-5.3.1-Linux-x86.sh" "https://repo.continuum.io/archive/Anacon da3-5.3.1-Linux-x86.sh#5dab8b2c95595df7fa55b88643f8372135c14faabd9ec05a34021551bb0999a1" "anaconda" verify_py36
4 ;;
5 "Linux-x86_64" )
6 install_script "Anaconda3-5.3.1-Linux-x86_64.sh" "https://repo.continuum.io/archive/Ana conda3-5.3.1-Linux-x86_64.sh#d4c4256a8f46173b675dd6a62d12f566ed3487f932bab6bb7058f06c124b cc27" "anaconda" verify_py36
7 ;;
8 "MacOSX-x86_64" )
9 install_script "Anaconda3-5.3.1-MacOSX-x86_64.sh" "https://repo.continuum.io/archive/An aconda3-5.3.1-MacOSX-x86_64.sh#23c373abce2463d4df495f5a1c7e8b0faec6eda09542d98f41ed65a0fa 0dbde0" "anaconda" verify_py36
10 ;;
11 * )
12 { echo
13 colorize 1 "ERROR"
14 echo ": The binary distribution of Anaconda3 is not available for $(anaconda_architec ture 2>/dev/null || true)."
15 echo
16 } >&2
17 exit 1
18 ;;
anaconda3-5.3.1linux版本的下载地址:https://repo.continuum.io/archive/Anaconda3-5.3.1-Linux-x86_64.sh,可以将其更改为中科大或清华的国内源位置:
# 中科大源
https://mirrors.ustc.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
# 清华源
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh
除此之外还有一种更简单值得推荐的方法
在~/.pyenv目录下新建名为cache的目录,把需要安装到软件通过国内源下载并放到cache目录下,通过pyenv install命令安装即可。
以Python3.7.1 为例,pyenv下载Python3.7.1地址为:https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
因此,可以通过Python官方网站下载后缀名为xz格式的Python-3.7.1.tar.xz文件并放置于~/.pyenv/cache目录下,执行 pyenv install 3.7.1安装Python-3.7.1。
如果出现不能安装Python的错误信息BUILD FEILED,可以通过执行以下命令:
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev
解决问题网址:
https://github.com/pyenv/pyenv/wiki
https://github.com/pyenv/pyenv/wiki/Common-build-problems
国内可用的Python软件源:
搜狐:http://mirrors.sohu.com/python/
anaconda软件源:
中科大:https://mirrors.ustc.edu.cn/anaconda/archive/
清华:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive
2 pyenv 常用命令
pyenv install --list # 列出可安装版本
pyenv install <version> # 安装对应Python版本
pyenv install -v <version> # 安装对应Python版本,若发生错误,显示详细的错误信息
pyenv versions # 显示当前使用的python版本
pyenv which python # 显示当前python安装路径
pyenv global <version> # 设置默认Python版本
pyenv local <version> # 在当前路径创建一个.python-version文件, 以后进入这个目录自动切换为该Python版本
pyenv shell <version> # 在当前shell的session中启用某Python版本,优先级高于global 及 local
3 虚拟环境使用
pyenv virtualenv env # 从默认版本创建虚拟环境
pyenv virtualenv 3.7.1 env-3.7.1 # 创建3.7.1版本的虚拟环境
pyenv activate env-3.7.1 # 激活 env-3.7.1这个虚拟环境
pyenv deactivate # 停用当前的虚拟环境
pyenv uninstall env-3.7.1 # 删除 env-3.6.4 这个虚拟环境
具体使用方法步骤
step1 创建基于Python-3.7.1的虚拟环境,环境名为env-3.7.1
$ pyenv virtualenv 3.7.1 env3.7.1
Looking in links: /tmp/tmpv63lu7ne
Requirement already satisfied: setuptools in /home/zhou/.pyenv/versions/3.7.1/envs/env3.7.1/lib/python3.7/site-packages (39.0.1)
Requirement already satisfied: pip in /home/zhou/.pyenv/versions/3.7.1/envs/env3.7.1/lib/python3.7/site-packages (10.0.1)
step2 在项目文件中使用pyenv local env3.7.1,在项目当前目录中创建.python-version文件并写入虚拟环境名
$ pyenv local env3.7.1
(env3.7.1) $ ls -al
总用量 12
drwxr-xr-x 2 zhou zhou 4096 12月 2 14:21 .
drwxr-xr-x 3 zhou zhou 4096 12月 2 14:14 ..
-rw-r--r-- 1 zhou zhou 9 12月 2 14:21 .python-version
4 在虚拟环境中pypi安装模块使用国内源(中科大源)
编辑~/.pip/pip.conf文件,如果没有就自行创建,添加如下内容:
[global]
index-url = https://mirrors.ustc.edu.cn/pypi/web/simple
format = columns
参考文献:
1 https://github.com/pyenv/pyenv
2 https://github.com/pyenv/pyenv-installer
3 https://www.jianshu.com/p/af1f8d7b6b31
网友评论