Python2 series is installed on Mac by default, but here we will explain how to install 3 series with brew
and how to build a virtual environment with venv
.
Install Python3 with homebrew
.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Previously it was necessary to explicitly specify python3
, but now it seems that 3 series is installed by default
brew install python
Execute the following, and if Python 3.7.3
etc. is displayed, the installation is successful.
Also, python
should select the 2nd series, and python3
should select the 3rd series.
python3 -V
mkdir project
cd project
python3 -m venv env
You can enable the virtual environment with the following command.
source env/bin/activate
With the virtual environment enabled, python -V
is used to link the 3 systems.
You can get out of the virtual environment with the following command.
deactivate
If you install Python3 and the library to be used differs depending on the project, it is essential to isolate the environment with venv
by the above procedure.
By using venv
, it is possible to prevent unnecessary libraries from being mixed in when writing out the list of libraries used in the project with pip freeze> requirements.txt
etc.
Previously, this article introduced the method using virtualenv
instead of venv
, so I will also describe it.
Install virtualenv
with pip3
.
pip3 install virtualenv
After moving to the project directory
virtualenv --python=/usr/local/bin/python3 env
Run the following in the project directory
source env/bin/activate