mac安装、管理多个Python版本

macOS自带Python版本是2.7,如果需要使用Python3可以通过brew轻松安装,但是,当我们需要为Python3安装一些模块的时候,系统会首先将模块安装到默认的2.7版本上去。有一个比较简单的处理方法,可以通过pyenv来对macOS上的多个Python版本进行安装和管理。

安装pyenv

brew install pyenv

安装完成后,可以使用pyenv命令查看安装版本和命令说明

~ pyenv
pyenv 1.2.17
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
   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
   prefix      Display prefix for a Python version
sed: RE error: illegal byte sequence
   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 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

检查本地Python版本

pyenv versions

返回结果:

* system (set by /Users/tangbincheng/.pyenv/version)

每一行代表一个Python版本,第一行的system代表系统自带版本。
“*” 代表当前使用的版本

安装其他Python版本

如果你还需要安装其他Python版本,先查看一下可安装的Python版本:

pyenv install --list

安装对应的版本

pyenv install 3.6.0

完成安装后继续使用上面的命令验证是否安装成功:

~ pyenv versions
* system (set by /Users/tangbincheng/.pyenv/version)
  3.6.0

切换要使用的Python版本

pyenv global 3.6.0

执行后:

~pyenv versions
  system
* 3.6.0 (set by /Users/tangbincheng/.pyenv/version)

设置环境变量

vi ~/.bash_profile

在文件底部添加如下配置:

export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/shims:$PATH

让修改后的配置文件生效

source ~/.bash_profile

验证一下:

~ python
Python 3.6.0 (default, Mar 29 2020, 19:12:59)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
发布了102 篇原创文章 · 获赞 6 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/tt75281920/article/details/105184297
今日推荐