Python installation 2020 (macOS)

Python has the following specifications as a standard function.

--Only one version can run on the local machine --Cannot change the version of Python executed for each project --Dependent libraries (libraries installed by pip) are installed on all local machines

--You cannot change the version of the dependent library for each project --I don't know which project's dependent library

If you're doing something like personal scraping, it doesn't really matter. However, these are problems when developing with multiple people or developing multiple projects.

These can be resolved by using tools for "version control" and "dependent library management".

This time, it's the way to install it on macOS.

Tools used this time

Install pyenv

"Pyenv" is a popular tool for switching versions.

pyenv

pyenv is a tool that allows you to switch between multiple versions of Python. Not only can you switch between versions on your local machine, but you can also specify a version of Python that runs under a particular directory.

Installation

Install from brew. (I will omit the installation method of brew itself)

brew install pyenv

You need to pass it after installation. This is an example of using zsh for the shell. If you are using bash, modify "\ ~ / .zshrc" to "\ ~ / .bashrc" and execute.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

Version switching

You can switch between installation and version with pyenv.

pyenv install 3.8.3 ## python3.8.Get 3
pyenv global 3.8.3 ##Switch the version used on the local machine

Install pipx

It's a tool you don't need to avoid dependencies between projects, but it's useful when installing commonly used dependent libraries.

pipx

If you install the dependent libraries for each project with poetry, you have to install the commonly used libraries (tools that are used directly from the OS, such as "aws cli", instead of using them from Python code) for each project. I have to. If you install the dependent libraries with the pipx command, you can use them in common from any project.

Installation

brew install pipx

After installation, pass the path.

echo 'export PATH=$PATH:~/.local/bin' >> ~/.zshrc

installation of poetry

Use poetry as a dependent library management tool.

poetry

poetry is a tool that installs dependent libraries in a location isolated from your normal installation. An isolated environment is called a virtual environment. You can even package when creating a library for distribution.

Installation

Install using pipx.

pipx install poetry

If you are not using pipx, install it from the installer script.

curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
python get-poetry.py

As for the installation destination of the virtual environment, it is better to create it in each project, so change the setting.

poetry config virtualenvs.in-project true

With this setting, a virtual environment will be created in the venv folder in the project.

How to create a project

Let's actually create a project.

Specify Python version

Specify the root directory as the project directory and Pyenv to specify the Python version.

pyenv local 3.8.3

Create pyproject.toml

This file will list the dependent libraries used in the project.

poetry init

Make sure you can run Python in the poetry virtual environment.

poetry run python -V

If the version is displayed, the installation is successful.

Add dependent library

Add the dependent libraries for your project.

poetry add {package name} ##Add dependent libraries to project
protry add --dev {package name} ##Added dependent libraries for development

Execute from command

Dependent libraries added from poetry can be executed from the poetry command.

poetry run {package name}

Small story

This is a small story that is a little useful for using Python.

Remove all dependent libraries

pip freeze > requirements.txt
sudo pip uninstall -r requirements.txt

Find out where to install dependent packages

pip is also a dependent library for Python, so you can find out by looking at the installation location of pip.

pip show pip

Location shows the installation location.

Recommended Posts

Python installation 2020 (macOS)
Python installation
OpenCV3 installation for Python3 @macOS
Python installation (Windows)
Python3.4 installation notes
python openCV installation (memo)
Python basic course (2 Python installation)
Reinforcement learning 1 Python installation
Python installation method Windows
Installation of Python 3.3 rc1
Python 2.7 installation (yum) (CentOS 6.8)
Installation of matplotlib (Python 3.3.2)
Python 3.5 installation (yum) (CentOS 6.8)
python (pyenv + pyenv-virtualenv) + CentOS7 installation
Python CMS Mezzanine installation procedure
Python 3.6 installation procedure [for Windows]
Python
Python installation and basic grammar
Made python available on macOS
Fastest Python installation on Windows
Python 3 series installation for mac
Ruby, Python Module Installation Guide
Python (Python 3.7.7) installation and basic grammar
Installation of SciPy and matplotlib (Python)
I tried to touch Python (installation)
Python practice_Virtual environment setup ~ Django installation
Python memo Anaconda installation (Mac only)
Python development environment construction on macOS
PIL (Python Imaging Library) installation memorandum
[2021 version] Python installation Windows 10 (64bit) edition
If Python 3.5.0 installation fails on Mac
Use gcc-4.2 when compiling Python (MacOS)
[Python] Make pip available on macOS
Installation of Visual studio code and installation of python
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python development environment for macOS using venv 2016
Python service
python tips
Django installation
Cuda installation
python function ①
Python basics
Python memo
ufo-> python (3)
boto3 installation
Python comprehension
install python
Python Singleton
Build a python environment on MacOS (Catallina)
Python basics ④
Python Memorandum 2
python memo