通过pyenv和virtualenv搭建python开发环境

1. 安装pyenv

pyenv 用来管理 Python 的版本,Python 2.x 和 Python 3.x版本之间灵活切换,virtualenv隔离每个项目包,结合pyenv管理Python的版本,不同项目需要依赖的包版本不同,则需要使用虚拟环境。

1.1 部署pyenv环境
[root@localhost ~]# git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/root/.pyenv'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 17361 (delta 1), reused 5 (delta 1), pack-reused 17352
Receiving objects: 100% (17361/17361), 3.38 MiB | 431.00 KiB/s, done.
Resolving deltas: 100% (11819/11819), done.
1.2 配合环境变量

写入bash_profile

### pyenv env PATH
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile

echo  'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile 

echo  'eval "$(pyenv init -)"' >> ~/.bash_profile

source ~/.bash_profile 

2. 命令使用

Usage: pyenv []

commands含义
commands显示所有的command
local显示本地的python版本
global设置或者显示python版本
shell设置或者显示shell目前使用的python版本
install安装python版本
uninstallU卸载指定python版本
rehashRehash pyenv shims (run this after installing executables)
version显示当前的python版本
versionsList all Python versions available to pyenv
whichDisplay the full path to an executable
whenceList all Python versions that contain the given executable
2.1 安装python版本
[root@localhost ~]# pyenv install 2.7.14
Downloading Python-2.7.14.tar.xz...
-> https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz
Installing Python-2.7.14...
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python zlib extension was not compiled. Missing the zlib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


BUILD FAILED (Red Hat Enterprise Linux Server 7.4 using python-build 1.2.13-2-g0aeeb6f)

Inspect or clean up the working tree at /tmp/python-build.20190823124027.2535
Results logged to /tmp/python-build.20190823124027.2535.log

Last 10 log lines:
rm -f /root/.pyenv/versions/2.7.14/share/man/man1/python.1
(cd /root/.pyenv/versions/2.7.14/share/man/man1; ln -s python2.1 python.1)
if test "xno" != "xno" ; then \
        case no in \
                upgrade) ensurepip="--upgrade" ;; \
                install|*) ensurepip="" ;; \
        esac; \
         ./python -E -m ensurepip \
                $ensurepip --root=/ ; \
fi

存在报错:ERROR: The Python zlib extension was not compiled. Missing the zlib?
解决方案:https://github.com/pyenv/pyenv/wiki/common-build-problems

  • 根据解决方案,安装相关的安装包
## 安装前,配置镜像源,配置epel源
yum install @development zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel findutils
[root@localhost ~]# pyenv install 2.7.14
Downloading Python-2.7.14.tar.xz...
-> https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tar.xz

Installing Python-2.7.14...
Installed Python-2.7.14 to /root/.pyenv/versions/2.7.14

python 3

[root@localhost ~]# pyenv install 3.5.4
Downloading Python-3.5.4.tar.xz...
-> https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz
Installing Python-3.5.4...
Installed Python-3.5.4 to /root/.pyenv/versions/3.5.4
2.2 查看当前python版本
2.3 设置版本

pyenv redhash --> 创建垫片路径,为已经安装可执行文件创建shims,每次增删python版本之后,都应该执行这次命令。

  • 设置当前shell默认的python版本
[root@localhost ~]# pyenv shell 2.7.14
[root@localhost ~]# python 
Python 2.7.14 (default, Aug 23 2019, 13:34:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • 设置本地版本local
pyenv local 2.7.14
pyenv rehash
  • 设置global全局版本
[root@localhost ~]# pyenv global 2.7.14
[root@localhost ~]# 
[root@localhost ~]# pyenv rehash 
[root@localhost ~]# pyenv versions
  system
* 2.7.14 (set by PYENV_VERSION environment variable)
  3.5.4

3. virtualenv管理项目

virtualenv是个独立的项目,用来隔离不同的项目的工作环境,比如在同一台主机上面,不同的项目需要使用不同版本的python

3.1 安装pyenv-virtualenv
[root@localhost ~]# git clone https://github.com/yyuu/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
Cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 2064, done.
remote: Total 2064 (delta 0), reused 0 (delta 0), pack-reused 2064
Receiving objects: 100% (2064/2064), 580.31 KiB | 211.00 KiB/s, done.
Resolving deltas: 100% (1413/1413), done.
[root@localhost ~]# echo $(pyenv root)
/root/.pyenv
[root@localhost ~]# echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
[root@localhost ~]# source ~/.bash_profile
[root@localhost ~]# 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
3.2 pyenv-virtualenv使用

为同一个python解释器,创建不同的工作环境

  • 创建两个项目,一个用python2,一个用python3
[root@localhost ~]# pyenv virtualenv 2.7.14 python2-learning
Collecting virtualenv
  Downloading https://mirrors.aliyun.com/pypi/packages/f7/69/1ad2d17560c4fc60170056dcd0a568b83f3453a2ac91155af746bcdb9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB)
    100% |████████████████████████████████| 3.3MB 323kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.4
You are using pip version 9.0.1, however version 19.2.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
New python executable in /root/.pyenv/versions/2.7.14/envs/python2-learning/bin/python2.7
Also creating executable in /root/.pyenv/versions/2.7.14/envs/python2-learning/bin/python
Installing setuptools, pip, wheel...
done.
Requirement already satisfied: setuptools in /root/.pyenv/versions/2.7.14/envs/python2-learning/lib/python2.7/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/2.7.14/envs/python2-learning/lib/python2.7/site-packages
[root@localhost ~]# pyenv virtualenv 3.5.4 python3-learning
Requirement already satisfied: setuptools in /root/.pyenv/versions/3.5.4/envs/python3-learning/lib/python3.5/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/3.5.4/envs/python3-learning/lib/python3.5/site-packages
[root@localhost ~]# ls
anaconda-ks.cfg epel-release-latest-7.noarch.rpm python.1 RedHat-7.4-x86_64-dvd.iso requirements.txt zhanghao
[root@localhost ~]# pyenv virtualenvs
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
  • 查看创建之后的工程目录

python2-learning (created from /root/.pyenv/versions/2.7.14)
python3-learning (created from /root/.pyenv/versions/3.5.4)

[root@localhost ~]# cd /root/.pyenv/versions/2.7.14
[root@localhost 2.7.14]# ls
bin envs include lib share
[root@localhost 2.7.14]# cd envs/
[root@localhost envs]# ls
python2-learning
[root@localhost envs]# cd python2-learning/
[root@localhost python2-learning]# ls
bin include lib
[root@localhost python2-learning]# 
  • 检验环境是否隔离
[root@localhost python2-learning]# pyenv virtualenvs
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
[root@localhost python2-learning]# pyenv activate python2-learning 
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(python2-learning) [root@localhost python2-learning]# pip install flask
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting flask
  Downloading https://mirrors.aliyun.com/pypi/packages/9b/93/628509b8d5dc749656a9641f4caf13540e2cdec85276964ff8f43bbb1d3b/Flask-1.1.1-py2.py3-none-any.whl (94kB)
     |████████████████████████████████| 102kB 2.8MB/s 
Collecting Werkzeug>=0.15 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/d1/ab/d3bed6b92042622d24decc7aadc8877badf18aeca1571045840ad4956d3f/Werkzeug-0.15.5-py2.py3-none-any.whl (328kB)
     |████████████████████████████████| 337kB 3.4MB/s 
Collecting itsdangerous>=0.24 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting click>=5.1 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
     |████████████████████████████████| 81kB 3.8MB/s 
Collecting Jinja2>=2.10.1 (from flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
     |████████████████████████████████| 133kB 3.8MB/s 
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->flask)
  Downloading https://mirrors.aliyun.com/pypi/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10.1 MarkupSafe-1.1.1 Werkzeug-0.15.5 click-7.0 flask-1.1.1 itsdangerous-1.1.0
(python2-learning) [root@localhost python2-learning]# 
(python2-learning) [root@localhost python2-learning]# python
Python 2.7.14 (default, Aug 23 2019, 13:34:53) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@localhost ~]# pyenv virtualenvs 
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
[root@localhost ~]# pyenv activate python3-learning 
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(python3-learning) [root@localhost ~]# python
Python 3.5.4 (default, Aug 23 2019, 14:12:04) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
  • 删除项目环境
[root@localhost ~]# pyenv virtualenvs
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  3.5.4/envs/python3-learning (created from /root/.pyenv/versions/3.5.4)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
  python3-learning (created from /root/.pyenv/versions/3.5.4)
[root@localhost ~]# pyenv virtualenv-delete python3-learning 
pyenv-virtualenv: remove /root/.pyenv/versions/3.5.4/envs/python3-learning? y
[root@localhost ~]# pyenv virtualenvs 
  2.7.14/envs/python2-learning (created from /root/.pyenv/versions/2.7.14)
  python2-learning (created from /root/.pyenv/versions/2.7.14)
[root@localhost ~]# 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值