Pyenv 使用指南

2,128 阅读1分钟

1、安装方法

详见官方文档:github.com/pyenv/pyenv…

推荐通过 Homebrew 安装:

$ brew install pyenv

然后将如下内容 添加到 ~/.profile~/.bash_profile~/.bash_login~/.zshrc 文件中:

export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"`asd ` 

执行如下命令,以便让环境变量生效:

exec "$SHELL"

最后可以执行如下命令验证下“是否安装成功”:

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

Some useful pyenv commands are:
   --version   Display the version of pyenv
   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-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

2、使用方法

2.1、安装指定版本的 Python

通过如下命令,可以安装 Python 3.11.2

$ pyenv install  3.11.2

安装完成后:

$ pyenv versions
* system (set by /Users/mengxinxin/.pyenv/version)
  3.11.2

$ pwd
/Users/mengxinxin/.pyenv

$ tree -l 3
232.47 MiB .pyenv
  3.29 KiB ├─ shims
   187   B │  ├─ 2to3
   187   B │  ├─ 2to3-3.11
   187   B │  ├─ idle
   187   B │  ├─ idle3
   187   B │  ├─ idle3.11
   187   B │  ├─ pip
   187   B │  ├─ pip3
   187   B │  ├─ pip3.11
   187   B │  ├─ pydoc
   187   B │  ├─ pydoc3
   187   B │  ├─ pydoc3.11
   187   B │  ├─ python
   187   B │  ├─ python-config
   187   B │  ├─ python3
   187   B │  ├─ python3-config
   187   B │  ├─ python3.11
   187   B │  ├─ python3.11-config
   187   B │  └─ python3.11-gdb.py
232.47 MiB └─ versions
232.47 MiB    └─ 3.11.2
105.48 KiB       ├─ bin
868.68 KiB       ├─ include
231.49 MiB       ├─ lib
 19.80 KiB       └─ share

2.2、切换 Python 到指定版本

$ pyenv global 3.11.2

$ pyenv versions
  system
* 3.11.2 (set by /Users/mengxinxin/.pyenv/version)