[PYTHON] Build a virtual environment with pyenv and venv

Since I sometimes built a Python environment, I will summarize it as a memorandum.

usage environment

Mac Catalina 10.15.6

What to use for building a virtual environment

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.

How to install pyenv

1. Install from Homebrew

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.

2. Create a .profile for pyenv

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.

Install python

1. Check Python

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.

2. Installation

Terminal.


pyenv install [The version that suits you]
Example) pyenv install 3.8.5

3. Set the selected Python version as the default

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.

1. Installation of virtualenv (python 2 series only)

Terminal.


pip install virtualenv

2. Move to project

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.

3. Create 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.

4. Activate the virtual environment

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

Build a virtual environment with pyenv and venv
Build a python virtual environment with pyenv
[Memo] Build a virtual environment with Pyenv + anaconda
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a numerical calculation environment with pyenv and miniconda3
python with pyenv and venv
Create a virtual environment with Anaconda installed via Pyenv
Build a Python virtual environment using venv (Django + MySQL ①)
Building and enabling a python virtual environment, etc. (venv)
Build a Django environment for Win10 (with virtual space)
Build python virtual environment with virtualenv
Create a virtual environment with Python!
Building a virtual environment with Python 3
Environment construction with pyenv and pyenv-virtualenv
Build a machine learning scikit-learn environment with VirtualBox and Ubuntu
Build a python environment with pyenv (OS X El Capitan 10.11.3)
Build a Python environment with WSL + Pyenv + Jupyter + VS Code
[Pyenv] Building a python environment with ubuntu 16.04
Easily build a development environment with Laragon
Build a Tensorflow environment with Raspberry Pi [2020]
Build a Fast API environment with docker-compose
Create a virtual environment with Python_Mac version
[Linux] Build a jenkins environment with Docker
Build a modern Python environment with Neovim
[Linux] Build a Docker environment with Amazon Linux 2
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a Python environment on your Mac with Anaconda and PyCharm
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a C language development environment with a container
Prepare a Python virtual environment for your project with venv with VS Code
Build python environment with pyenv on EC2 (ubuntu)
Building a python environment with virtualenv and direnv
Build a python environment with ansible on centos6
How about creating a virtual environment with Anaconda and doing pip install?
Start Django in a virtual environment with Pipenv
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Build a Django environment with Vagrant in 5 minutes
Build a CentOS Linux 8 environment with Docker and start Apache HTTP Server
Build a Django development environment with Doker Toolbox
[Venv] Create a python virtual environment on Ubuntu
Work in a virtual environment with Python virtualenv.
Python virtual environment construction (2017 version) pyenv and pyenv-virtualenv and virtualenv and virtualenv wrapper and pyvenv and venv
Flow of creating a virtual environment with Anaconda
Build a Python environment with OSX El capitan
Build python3.x with pyenv
Quickly build a Python Django environment with IntelliJ
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Build a Python virtual environment that anyone can understand September 2016 (pyenv + virutalenv)
Build a drone simulator environment and try a simple flight with Mission Planner
Build PyPy and Python execution environment with Docker
Virtual environment with Python 3.6
Build a Python machine learning environment with a container
Build a python execution environment with VS Code
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Build a data analysis environment that links GitHub authentication and Django with JupyterHub
Build a TensorFlow development environment on Amazon EC2 with command copy and paste
Build a distributed environment with Raspberry PI series (Part 3: Install and configure dnsmasq)
A memo to create a virtual environment (venv) before Django
code-server Online environment (2) Create a virtual network with Boto3