Mac install and manage multiple versions of Python

background

In MacOS 10.15 Catalina is about to open beta on the occasion, foreign technology forum published an article entitled " macOS 10.15 Catalina abandoned UNIX scripting language " posts, where it says: older Python language version 2.7 has been abandoned in the MacOS 10.15 Catalina use, will not be included in the MacOS 10.16 in. Other UNIX scripting languages (Ruby & Perl) as well.

Then in 10.14 and earlier versions of MacOS, it is built in Python2 version. In this I also recommend Python learners learn directly Python3.

For the Mac, students learn Python, the installation can not avoid the double version of Python and Python version of handover situation. So how effective fast switching it? Many students will think modify environment variables, specify the default path of Python, of course, this problem can be solved, but it is not elegant, simple enough, not fast enough. At this point, pyenv came into being, it is a Python version management tool , it can change the global version of Python, install multiple versions of Python, set the directory level version of Python, Python can also create and manage virtual environments (virtual python environments) .

PS: I experience a bit MacOS 10.15 beta version, do a bunch of BUG, ​​not recommended upgrade.

Installation & Use

1, install Homebrew

Official website address: brew.sh/ obtain installation instructions to install:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
复制代码

Homebrew After a successful installation, it will automatically create the directory / usr / local / Cellar Homebrew installed programs to store

PS: Homebrew is one of the tools will be installed under MacOS, is a very efficient command-line package manager

2, installation pyenv

brew update
brew install pyenv
pyenv -v # 安装之后查看 pyenv 版本,确认是否安装成功
复制代码

3, a plurality of mounting management & Python

pyenv install 2.7.15
pyenv install 3.7.3
pyenv versions # 所有已经安装的版本
复制代码

Note: MacOS 10.14, the following error may occur:

zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
复制代码

the reason:

Details, refer to: track down the problem analysis

solution:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
# 此时再安装试试
pyenv install 3.7.3
# 查看所有已经安装的版本,注:星号指定当前的版本
pyenv versions
复制代码

PS: default installation path: ~ / .pyenv / shims / python

4, commonly used commands

使用方式: pyenv <命令> [<参数>]

命令:
  commands    查看所有命令
  local       设置或显示本地的 Python 版本(当前目录及其子目录)
  global      设置或显示全局 Python 版本
  shell       设置或显示 shell 指定的 Python 版本(本次会话)
  install     安装指定 Python 版本
  uninstall   卸载指定 Python 版本)
  version     显示当前的 Python 版本及其本地路径
  versions    查看所有已经安装的版本
  which       显示安装路径
复制代码

Switch Version

pyenv global 3.7.3 # 不建议全局切换
python -V  # 验证一下是否切换成功
pyevn global system  # 切换回系统版本
pyenv local 3.7.3  # 当前目录及其目录切换
python -V  # 验证一下是否切换成功
pyenv local --unset  # 解除local设置
pyenv shell 3.7.3  # 当前shell会话切换
python -V  # 验证一下是否切换成功
pyenv shell --unset  # 解除shell设置
复制代码

Switching fails

If you encounter after the switch, the default version of Python version or system, then you need to configure the environment variables, and finally write in ~ / .zshrc or ~ / .bash_profile file:

export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/shims:$PATH
if which pyenv > /dev/null;
  then eval "$(pyenv init -)";
fi
复制代码

Validate the configuration

source ~/.zshrc
# or
source ~/.bash_profile
复制代码

PS: Use pyenv combined with the virtual environment will be more powerful, it strongly recommended.


Development: Installation and use of pyenv-virtualenv

pyenv-virtualenv is a pyenv plug-ins that can be used to build based on different versions of Python, python virtual environment and independent , you can make each project environment and other projects open to independent, maintain a clean environment, to resolve package conflicts .

installation

brew update
brew install pyenv-virtualenv
复制代码

Profiles

In ~ / .zshrc or ~ / .bash_profile file last written:

# pyenv-virtualenv
if which pyenv-virtualenv-init > /dev/null;
  then eval "$(pyenv virtualenv-init -)";
fi
复制代码

to validate

source ~/.zshrc
# or
source ~/.bash_profile
复制代码

use

1. Create virtualenv

Virtualenv created using the current version

# 当前版本为 2.7.15
pyenv virtualenv xxx-2.5.15
复制代码

Specifies the version created virtualenv

#pyenv virtualenv 版本号 虚拟环境名
pyenv virtualenv 3.7.3 test-3.7.3
复制代码

2, view the created virtualenv

pyenv versions
复制代码

3, activation and deactivation virtualenv

Manually activate & deactivate:

# 激活
pyenv activate test-3.7.3
# 停用
pyenv deactivate
复制代码

Automatic activation:

# 方式一:
# 手动把`虚拟环境名`写入当前目录的.python-version文件中
vim .python-version
# 启动自动激活
pyenv activate test-3.7.3
# 停用自动激活
pyenv deactivate

# 方式二(推荐):
# 使用 pyenv local 虚拟环境名 (会自动将虚拟环境名写入到当前目录的.python-version文件中)
pyenv local test-3.7.3
# 停用并取消自定激活
pyenv local --unset
复制代码

4, delete the existing virtualenv

pyenv uninstall test-3.7.3 # 使用 pyenv uninstall 虚拟环境名
y # 输入 y ,然后回车
复制代码

Reproduced in: https: //juejin.im/post/5cfcc9a7e51d45775d516f55

Guess you like

Origin blog.csdn.net/weixin_33739627/article/details/91429028