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.
"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.
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
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
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.
brew install pipx
After installation, pass the path.
echo 'export PATH=$PATH:~/.local/bin' >> ~/.zshrc
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.
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.
Let's actually create a project.
Specify the root directory as the project directory and Pyenv to specify the Python version.
pyenv local 3.8.3
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 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
Dependent libraries added from poetry can be executed from the poetry command.
poetry run {package name}
This is a small story that is a little useful for using Python.
pip freeze > requirements.txt
sudo pip uninstall -r requirements.txt
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