background

MacOS 10.15 Catalina will soon be in beta, but the UNIX Scripting language will be abandoned for MacOS 10.15 Catalina. The older Python language version 2.7 was deprecated in MacOS 10.15 Catalina and will not be included in MacOS 10.16. The same is true for other UNIX scripting languages (Ruby & Perl).

And then on MacOS 10.14 and before that, there’s Python2 built in. I also recommend that Python learners learn Python3 directly.

For those of you who are learning Python on a Mac, it is unavoidable to install two versions of Python and switch between Python versions. So how to switch effectively and quickly? Many of you would think of modifying environment variables to specify Python’s default path, which would certainly solve the problem, but it’s not elegant, it’s not neat, it’s not fast. Pyenv is a Python versioning tool that allows you to change global Python versions, install multiple versions of Python, and set directory-level Versions of Python. You can also create and manage Virtual Python environments.

PS: I tried the MacOS 10.15 beta, and it was full of bugs. I don’t recommend upgrading.

Installation & Use

1. Install Homebrew

Brew. Sh/To obtain the installation instructions and install:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code

After Homebrew is successfully installed, the /usr/local/CELLAR directory is automatically created to store the Homebrew installed programs

PS: Homebrew is one of the necessary tools for MacOS. It is a very efficient command line package manager

2. Install PyEnv

Brew update brew install pyenv pyenv -vCopy the code

Install and manage multiple Pythons

Pyenv Install 2.7.15 PyEnv Install 3.7.3 PyEnv Versions #Copy the code

Note: In MacOS 10.14, the following error may occur:

zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1
Copy the code

The reason:

Detailed reference: problem tracing analysis

Solution:

Sudo installer - PKG/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10. 14. PKG - target /#Try installing again at this pointPyenv install 3.7.3#View all installed versions, note: the asterisk specifies the current version
pyenv versions
Copy the code

PS: The default installation path is ~/. Pyenv /shims/python

4. Common commands

Pyenv < command > [< parameters >] command: commands View all commandslocalSet or display the local Python version (current directory and its subdirectories) global Set or display the global Python version Shell Set or display the Shell specified Python version (for this session) Install Install the specified Python version Uninstall Uninstall the specified Python version. Version Displays the current Python version and its local path. Versions View all installed versionswhichDisplay installation PathCopy the code

Switch version

Pyenv global 3.7.3 pyenv global 3.7.3 pyenv global 3.7.3 pyenv global 3.7.3 pyenv global 3.7.3 Pyenv local --unset pyenv shell 3.7.3 # Python -v # Pyenv shell --unset # unset shell SettingsCopy the code

Switchover failed

If the Python version is still the default after the switch, you need to configure the environment variable to write at the end of the ~/.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
Copy the code

Make the configuration take effect

source ~/.zshrc
# or
source ~/.bash_profile
Copy the code

PS: Using PyEnv in conjunction with virtual environments is much more powerful and highly recommended.


Extension: Installation and use of Pyenv-VirtualEnv

Pyenv-virtualenv is a pyEnv plug-in that can be used to build virtual Python environments based on different Python versions. It can be used to separate each project environment from other projects to keep the environment clean and solve package conflicts.

The installation

brew update
brew install pyenv-virtualenv
Copy the code

The configuration file

At the end of the ~/.zshrc or ~/.bash_profile file write:

# pyenv-virtualenv
if which pyenv-virtualenv-init > /dev/null;
  then eval "$(pyenv virtualenv-init -)";
fi
Copy the code

To take effect

source ~/.zshrc
# or
source ~/.bash_profile
Copy the code

use

1. Create virtualenv

Create virtualEnv with the current version

#The current version is 2.7.15- 2.5.15 pyenv virtualenv XXXCopy the code

Create virtualenv for the specified version

#Pyenv VirtualEnv Version Name of the virtual environmentPyenv virtualenv 3.7.3 test - 3.7.3Copy the code

2. View the created VirtualEnv

pyenv versions
Copy the code

3. Activate and deactivate VirtualEnv

Manual Activation & Deactivation:

#The activationPyenv activate the test - 3.7.3#disable
pyenv deactivate
Copy the code

Automatic activation:

#A:
#Manually write the 'virtual environment name' to the. Python-version file in the current directory
vim .python-version
#Enable automatic activationPyenv activate the test - 3.7.3#Disable automatic activation
pyenv deactivate

#Method 2 (Recommended) :
#Using pyenvlocalVirtual environment name (the virtual environment name is automatically written to the current directory in the. Python-version file)Pyenv local test - 3.7.3#Deactivate and deactivate custom activation
pyenv local --unset
Copy the code

4. Delete the existing Virtualenv

pyenv uninstall test- 3.7.3Pyenv uninstall Virtual environment name
y Type y, then press Enter
Copy the code