python版本管理工具:pyenv安装使用方法

1,290次阅读
没有评论
python版本管理工具:pyenv安装使用方法

pyenv是一个forked自ruby社区的简单、低调、遵循UNIX哲学的Python环境管理工具, 它可以轻松切换全局解释器版本, 同时结合vitualenv插件可以方便的管理对应的包源,可以让你轻松地在多个版本的 Python 之间切换。它简单而优雅,目的单一。

第一步:centos7 安装pyenv。

# 1、安装依赖 [root@richerdyoung] # 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

# 2、安装pyenv包 [root@richerdyoung] # git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# 3、设置环境变量 [root@richerdyoung] # vim ~/.bashrc

export PYENV_ROOT=”$HOME/.pyenv” export PATH=”$PYENV_ROOT/bin:$PATH” eval “$(pyenv init -)”

第二步:“:wq”保存并退出!并执行如下命令:

# 刷新配置 [root@richerdyoung] # source ~/.bashrc

# 即是启动语句,重启系统执行这条语句 [root@richerdyoung] # exec bash

第三步:查看结果,pyenv是否安装成功?毫无疑问是安装成功的。

[root@richerdyoung] # 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

注意:

当直接执行安装命令时候,国内可能会报错“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# 我的目录是 /root/.pyenv # 则下载好的python包 存放 ~/.pyenv/cache/Python-3.5.7 ~/.pyenv/cache/Python-3.5.8

pyenv常用的命令:

  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 mnt]# pyenv global system #将当前的全局Python版本还原到之前的版本

[root@richerdyoung mnt]# python -V #系统自带的Python版本

Python 2.7.5

[root@richerdyoung mnt]# pyenv version #检查 pyenv当前python版本

system (set by /root/.pyenv/version)

[root@richerdyoung mnt]# mkdir ops #创建一个测试目录

[root@richerdyoung mnt]# cd ops/

[root@richerdyoung ops]# pyenv local 3.5.7 #使用local子命令指定当前目录使用3.5.7版本

[root@richerdyoung ops]# pyenv local # 确认是否设置成功

3.5.7

[root@richerdyoung ops]# python -V #检查当前版本

Python 3.5.7

[root@richerdyoung ops]# cd #切换到其他目录

[root@richerdyoung ~]# python -V #再次检查Python版本 ops目录下版本为3.5.7 全局为2.7.5 符合预期

Python 2.7.5

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-03-01发表,共计2981字。
新手QQ群:570568346,欢迎进群讨论 Python51学习