Since I sometimes built a Python environment, I will summarize it as a memorandum.
Mac Catalina 10.15.6
What is often used to build a virtual environment
There are various things such as, but this time I would like to build with pyenv and venv which can be easily done. Then, it is each installation method.
pyenv A tool for managing python versions. This is an excellent one that can manage both Python 2 series and Python 3 series at the same time.
Terminal.
brew install pyenv
If you don't have Homebrew installed
Terminal.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
You can run it with to install Homebrew.
Put it in your PATH for pyenv. Create .zshrc directly under your home directory and write as follows. By the way
Terminal.
cd
pwd
You can find your home directory with.
/Users/username/.zshrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
When you reach here, close the terminal once.
Launch again,
Terminal.
pyenv -help
If no error occurs, the pyenv installation is complete.
Let's check the version of Python that can be currently installed by pyenv.
Terminal.
pyenv install --list
Choose the one that suits you from the listed versions.
Terminal.
pyenv install [The version that suits you]
Example) pyenv install 3.8.5
Make sure it is installed correctly.
Terminal.
pyenv versions
system
3.8.5
It is OK if the one you installed earlier is displayed like this. Now let's set the installed version as the default.
Terminal.
pyenv global [The version that suits you]
Example) python global 3.8.5
Let's check.
Terminal.
pyenv versions
system
* 3.8.5 (set by /Users/username/.pyenv/version)
It is OK if it is set like this.
Check the Python version for the time being.
Terminal.
python -V
3.8.5
It's perfect if it changes like this.
Also, it may be a slapstick, but it may not be possible to use it with Jupyter etc. as it is, so it is recommended to update pip.
Terminal.
pip install --upgrade pip
venv (virtualenv) Both venv and virtualenv are tools that allow you to manage Python module libraries on a project-by-project basis. However, while the Python 3 series originally has venv installed, the Python 2 series requires virtualenv to be installed. The basic part remains the same, so this time we will follow the Python 3 series.
Terminal.
pip install virtualenv
Terminal.
cd path/to/project
In this way, it seems customary to create it directly under the project to which you want to apply the virtual environment.
Terminal.
* Python 2 series
virtualenv [Virtual environment name to create]
* Python 3 series
python -m venv [Virtual environment name to create]
The virtual environment is now created.
Apparently it is customary to create it with .venv
.
There is no point in having a virtual environment. It is the same as the weapon of Dragon Quest. Activate is the equipment in Dragon Quest. Execute the following command directly under the project.
Terminal.
source [Virtual environment name]/bin/activate
Or
. [Virtual environment name]/bin/activate
Example) . .venv/bin/activate
This will make the terminal
(Virtual environment name): ~
Example) (.venv): ~
If so, you can activate it.
If you pip install
in this state, everything will be accumulated in venv, so the local environment will not be polluted.
By the way, deactivation is
Terminal.
deactivate
Is OK.
Recommended Posts