CentOS配置python版本管理工具pyenv

CentOS配置python版本管理工具pyenv

  • 1.安装工具依赖
  • 2.克隆python管理工具pyenv
  • 3.配置环境变量
  • 4.让环境生效
  • 5.安装python指定版本
  • 6.pyenv的简单使用

前言:每次配置Python环境都比较麻烦,遇到版本不兼容问题更复杂,且多次安装不同版本python版本也不好管理,所有我推荐使用Python的管理工具Pyenv,它可以方便地管理Python的不同版本。通过Pyenv,您可以轻松地切换和安装不同的Python版本。这样,您就可以避免版本兼容性的问题,并且更好地管理Python环境。

1.安装工具依赖

bash 复制代码
yum install -y git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

2.克隆python管理工具pyenv

bash 复制代码
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

3.配置环境变量

bash 复制代码
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

4.让环境生效

bash 复制代码
source ~/.bashrc

5.安装python指定版本

bash 复制代码
pyenv install 3.7.7

安装可能会比较慢,这边推荐一个快一点的方法

  1. 先在浏览器把python-3.7.7的包下载下来
    https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tar.xz
  2. 然后把压缩包上传到 /root/.pyenv/cache/
    因为pyenv install 3.7.7会先查看cache中是否存在,有就直接安装,不用再去拉取
  3. 再次安装即可
    pyenv install 3.7.7
  4. 选择python3.7.7
    pyenv global 3.7.7

6.pyenv的简单使用

bash 复制代码
[10:09:30 root@hadoop101 ~]#pyenv --help
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
   latest      Print the latest installed or known version with the given prefix
   local       Set or show the local application-specific Python version(s)
   prefix      Display prefixes for Python versions
   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 Python versions
   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

常用的如下:

bash 复制代码
# 查看已安装的所有python版本
pyenv versions
# 查看正在使用的python版本
pyenv version
# 下载指定python版本
pyenv install 3.7.7
# 选择使用指定python版本
pyenv global 3.7.7
相关推荐
紫雾凌寒30 分钟前
计算机视觉 |解锁视频理解三剑客——TimeSformer
python·深度学习·神经网络·计算机视觉·transformer·timesformer
陆沙1 小时前
deepseek-r1-centos-本地服务器配置方法
linux·服务器·centos
程序员杰哥1 小时前
测试用例详解
自动化测试·软件测试·python·功能测试·测试工具·职场和发展·测试用例
go54631584657 小时前
本地部署 GitHub 上的 Python 人脸识别项目
开发语言·python·github
FreakStudio8 小时前
手把手教你用 MicroPython 玩转幻尔串口舵机,代码+教程全公开
python·嵌入式·大学生·面向对象·技术栈·电子diy·电子计算机
tekin8 小时前
基于 Python 开发在线多人游戏服务器案例解析
服务器·python·游戏·在线多人游戏服务器
让学习成为一种生活方式10 小时前
libGL.so.1: cannot open shared object file: No such file or directory-linux022
linux·开发语言·python
java1234_小锋10 小时前
一周学会Flask3 Python Web开发-Jinja2模板继承和include标签使用
python·flask·flask3
图书馆钉子户10 小时前
from flask_session import Session 为什么是Session(app)这么用?
python·flask·mybatis
一只蜗牛儿10 小时前
Sherpa-ONNX:说话人识别与语音识别自动开启(VAD)+ Python API 完整指南
人工智能·python·语音识别