python版本管理工具pyenv

依然范儿特西
2021-08-26 / 0 评论 / 133 阅读 / 正在检测是否收录...

项目地址

https://github.com/pyenv/pyenv

简介:

python的版本管理工具:pyenv,他支持python多版本共存,并可以随时切换。且不会互相影响。

centos7 安装pyenv:

# 安装依赖
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel -y

# 安装pyenv包
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# 设置环境变量
vim ~/.bashrc 
export PYENV_ROOT="$HOME/.pyenv" 
export PATH="$PYENV_ROOT/bin:$PATH" 
eval "$(pyenv init -)"

:wq 保存退出!

# 刷新配置
source ~/.bashrc
# 即是启动语句,重启系统执行这条语句
exec bash

查看结果:

pyenv -h 
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   exec        Run an executable with the selected Python version
   global      Set or show the global Python version(s)
   help        Display help for a command
   hooks       List hook scripts for a given pyenv command
   init        Configure the shell environment for pyenv
   install     Install a Python version using python-build
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefix for a Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   root        Display the root directory where versions and shims are kept
   shell       Set or show the shell-specific Python version
   shims       List existing pyenv shims
   uninstall   Uninstall a specific Python version
   version     Show the current Python version(s) and its origin
   --version   Display the version of pyenv
   version-file   Detect the file that sets the current pyenv version
   version-name   Show the current Python version
   version-origin   Explain how the current Python version is set
   versions    List all Python versions available to pyenv
   whence      List all Python versions that contain the given executable
   which       Display the full path to an executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

安装成功~

简单的介绍一下常用的命令:

pyenv install --list        //查看可安装的python版本
pyenv install 3.5.7       //安装python3.5.7
pyenv rehash                    //更新数据库,在安装 Python 或者其他带有可执行文件的模块之后,需要对数据库进行更新:
pyenv versions        //查看当前使用的python版本
pyenv  global  3.5.7       //切换python全局版本为3.5.7
pyenv uninstall  3.5.7     //卸载已安装的python3.5.7

某个目录指定版本

[root@richerdyoung.com mnt]# pyenv global system   #将当前的全局Python版本还原到之前的版本
[root@richerdyoung.com mnt]# python -V  #系统自带的Python版本
Python 2.7.5
[root@richerdyoung.com mnt]# pyenv version  #检查 pyenv当前py 版本
system (set by /root/.pyenv/version)
[root@richerdyoung.com mnt]# mkdir ops #创建一个测试目录
[root@richerdyoung.com mnt]# cd ops/
[root@richerdyoung.com ops]# pyenv local 3.5.7  #使用local子命令指定当前目录使用3.5.7版本
[root@richerdyoung.com ops]# pyenv local   # 确认是否设置成功
3.5.7
[root@richerdyoung.com ops]# python -V #检查当前版本
Python 3.5.7
[root@richerdyoung.com ops]# cd #切换到其他目录
[root@richerdyoung.com ~]# python -V #再次检查Python版本 ops目录下版本为3.5.7   全局为2.7.5 符合预期
Python 2.7.5

ps : 当直接执行安装命令时候,国内会报错,

error: failed to download Python-3.5.7.tar.zx

解决办法:

在当前用户目录下 .pyenv/ 目录下创建 cache 目录,将下载好的 Python-3.5.7 的包放在该目录下,就不会去下载Python文件,直接执行安装,而不需要下载,节省下载时间

举例:


# 我的目录是
/root/.pyenv
# 则下载好的python包 存放

~/.pyenv/cache/Python-3.5.7
~/.pyenv/cache/Python-3.5.8
1

评论 (0)

取消