pyenv虚拟环境管理python多版本和软件库的方法
作者:singvis 时间:2022-07-18 07:56:30
可能大家在日常工作中会遇到这么个问题,现在基本的linux系统都是自带老版本的python2.7.x版本,我又不想用老版本,但直接升级可能会出问题,或是依赖老版本的程序就运行不了,有没办法能安装3.x新版本的?
答案是有的,请使用pyenv,可以支持管理多个python版本,任意使用。
其实官方于2019年底将不再提供支持了,截至倒计时还有6天:https://pythonclock.org/
那有人可能会问,那在一个系统下,不同的库依赖不同的python版本,就比如说python2.x版本需要django1.0版本,python3.x版本需要django2.0版本,那这样系统只能装一个django版本,这又怎么解决呢?
答案是有的,pyenv自带的插件pyenv-virtualenv,创建两个虚拟环境,互相独立,各不影响。
实验环境:
linux系统:CentOS 7 x64
pyenv常用命令:
[python@localhost ~]$ pyenv install -l //查看可用的安装版本
[python@localhost ~]$ pyenv install 3.6.9 //在线安装python3.6.9版本
[python@localhost ~]$ pyenv virtualenv 3.6.9 py3 //创建虚拟环境,3.6.9为python版本,py3为别名
[python@localhost test]$ pyenv local 3.6.9 //进入目录,设置或显示本地python版本(本目录有效)
[python@localhost test]$ pyenv global system //设置或显示全局python版本
[python@localhost test]$ pyenv version //显示当前python版本
[python@localhost test]$ pyenv versions //显示可用的所有python版本
[python@localhost test]$ pyenv update //更新pyenv
[python@localhost ~]$ pyenv virtualenvs //查看所有虚拟环境
[python@localhost ~]$ rm -fr ~/.pyenv //卸载pyenv
[python@localhost ~]$ pyenv virtualenv-delete py3 //删除虚拟环境
创建用户名及密码:
说明:不要用root账户,养成习惯
[root@localhost ~]$ useradd python
[root@localhost ~]$ su - python
[python@localhost ~]$ echo python | passwd python --stdin
安装依赖组件:
[python@localhost ~]$ yum install gcc make patch gdbm-devel openssl-devel sqlite-devel readline-devel
zlib-devel bzip2-devel git curl
安装pyenv:
#方法1:在线安装
[python@localhost ~]$ curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
#方法2:为避免受制墙限制而无法在线安装,复制如下链接的shell脚本到本地pyenv.sh:
https://github.com/pyenv/pyenv-installer/blob/master/bin/pyenv-installer
[python@localhost ~]$ touch pyenv.sh //新建文件,把上面的shell代码贴进来,保存
[python@localhost ~]$ bash pyenv.sh
安装完成后,按照提示,设置系统环境变量,在 '.bashrc' 增加如下代码:
[python@localhost ~]$ vim .bashrc
#最下面,新增如下脚本
export PATH="/home/python/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
重启shell,使路径更改生效:
[python@localhost ~]$ exec $SHELL
提前在官方下载python安装包并通过winSCP上传到centos目录下:
Python-3.6.9.tar.xz
Python-3.8.0.tar.xz
#在.pyenv目录下创建cache目录,已通过winSCP上传好
[python@localhost ~]$ cd .pyenv
[python@localhost .pyenv]$ mkdir cache
[python@localhost cache]$ ll
总用量 34224
-rw-rw-r--. 1 python python 17212164 12月 15 01:56 Python-3.6.9.tar.xz
-rw-rw-r--. 1 python python 17829824 12月 15 01:57 Python-3.8.0.tar.xz
备注:如果直接通过'pyenv install 3.6.9 '去安装,会很慢,因为要连接到国外。
开始安装
[python@localhost pkg]$ pyenv install 3.6.9 -vvv //-v 可以看到安装详细的过程
[python@localhost ~]$ pyenv install 3.8.0 -vvv //-v 可以看到安装详细的过程
创建项目文件夹:
#举个栗子:
#1.Python目录,使用python3.6.9版本
#2.My_django目录,使用python3.6.9版本
#3.test目录,使用python3.8.0版本
[python@localhost Python]$ mkdir -pv network/Python/
[python@localhost Python]$ mkdir -pv network/My_django/
[python@localhost Python]$ mkdir -pv network/test/
创建虚拟环境,效果如下:
#Python目录:
[python@localhost ~]$ pyenv virtualenv 3.6.9 py3 //设置别名py3
[python@localhost Python]$ pyenv local py3
(py3) [python@localhost Python]$ pyenv version
py3 (set by /home/python/network/Python/.python-version)
(py3) [python@localhost Python]$ //左边看到(py3)说明已在虚拟环境中,是独立的
#My_django目录:
[python@localhost ~]$ pyenv virtualenv 3.6.9 py369 //设置别名py369
[python@localhost My_django]$ pyenv local py369
(py369) [python@localhost My_django]$ pyenv version
py369 (set by /home/python/network/My_django/.python-version)
(py369) [python@localhost My_django]$
#test目录:
[python@localhost ~]$ pyenv virtualenv 3.8.0 py380 //设置别名py380
[python@localhost test]$ pyenv local py380
(py380) [python@localhost test]$ pyenv version
py380 (set by /home/python/network/test/.python-version)
(py380) [python@localhost test]$
#验证效果
#在My_django虚拟环境下安装pip install redis
#其他虚拟目录没有redis
#My_django虚拟环境
(py369) [python@localhost My_django]$ pip list
Package Version
---------- -------
pip 19.3.1
redis 3.3.11
setuptools 40.6.2
#Python虚拟环境
[python@localhost Python]$ pip list
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
说明:你可以在每个项目安装所需要的软件版本,都是独立的,互不影响。
pyenv官方链接
来源:https://segmentfault.com/a/1190000021402858
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python xlwt设置excel单元格字体及格式
![](https://img.aspxhome.com/file/2023/1/70021_0s.png)
Python中如何获取类属性的列表
python爬虫调度器用法及实例代码
![](https://img.aspxhome.com/file/2023/6/76936_0s.png)
Bootstrap实现渐变顶部固定自适应导航栏
![](https://img.aspxhome.com/file/2023/8/56078_0s.png)
浅析Python面向对象编程
月影:function扩展
CSS分栏布局的方法:绝对定位和浮动
![](https://img.aspxhome.com/file/UploadPic/20094/30/241003206401-26s.gif)
Python安全获取域管理员权限几种方式操作示例
![](https://img.aspxhome.com/file/2023/6/89236_0s.png)
介绍Python的Urllib库的一些高级用法
![](https://img.aspxhome.com/file/2023/5/93825_0s.png)
python opencv pytesseract 验证码识别的实现
![](https://img.aspxhome.com/file/2023/9/121439_0s.png)
Python使用captcha制作验证码的实现示例
![](https://img.aspxhome.com/file/2023/3/75113_0s.png)
Python 程序报错崩溃后如何倒回到崩溃的位置(推荐)
![](https://img.aspxhome.com/file/2023/1/83991_0s.jpg)
HTML语言将加入硬件操作功能
![](https://img.aspxhome.com/file/UploadPic/200912/16/01-55s.jpg)
基于javascript实现最简单的选项卡切换效果
laravel学习教程之存取器
Python实现的百度站长自动URL提交小工具
详解前端自动化工具gulp自动添加版本号
Django model序列化为json的方法示例
asp如何验证字符串是否包含有效字符?
python用Configobj模块读取配置文件
![](https://img.aspxhome.com/file/2023/7/110427_0s.png)