[PYTHON] Mechanism of pyenv and virtualenv

Purpose of this article

Explain how pyenv and virtualenv create a Python execution environment. There is no particular mention of how to install or use it. The explanation is a translation of the reference site, and there is no further information.

Reference site

yyuu/pyenv virtualenv

pyenv

Overview

A tool for using different python versions within the same terminal. You can set the python version to use for each working directory.

How it works

Component

Each version of python installed by pyenv is saved as below. By the way, the version name specified by pyenv is the same as the folder name.

pyenv_structure


$HOME/.pyenv/shims/               #The core of how pyenv works. Each shim command is placed.
          |-/versions/2.7.8/      #Installed Python interpreter
                   |-/3.4.2/      #2 as an example.7.8, 3.4.2, pypy-2.4.If you install 0 it will look like this
                   |-/pypy-2.4.0/
          |-/version              #Python version information file used in global

You can also use the python local command to place the .python-version file in any working directory.

The python global command edits the $ HOME / .pyenv / version file.

As the name suggests, the .python-version and $ HOME / .pyenv / version files only describe the version information of python to be used.

And the shim command is created under $ HOME / .pyenv / shims /. This will be explained in the next section.

shim and version switching mechanism

The shim command is a group of commands that have the same name as the python command (python, pip, etc.) and are placed in $ HOME / .pyenv / shims /. In a nutshell, it is a command that executes the command with the same name of the corresponding version after searching for version information. The specific procedure is as follows.

  1. Examine the environment variable PYENV_VERSION and use the version information, if any.

  2. If there is no information in the environment variable PYENV_VERSION, if the .python-version file is in the same directory, use the version described in it.

  3. If the .python-version file does not exist in the same directory, search the parent directory for the .python-version file.

  4. If the version information is not found in the above search, use the information described in the ~ / .pyenv / version file. If you don't even have this file, use Python in the system standard.

To use this shim command, you must set $ HOME / .pyenv / shims / to the left of the PATH where the system standard commands are located.

Example


~/.pyenv/shims:/usr/local/bin:/usr/bin:/bin

By the way, pyenv rehash is a command to reconfigure this shims command based on the currently installed python versions.

virtualenv

Directory built with virtualenv

You can create a Python runtime environment in any directory with the virtualenv command. This directory structure is simply the same as the Python interpreter directory. Python commands seem to refer to the include, lib directories in their parent directory. So virtualenv just creates that directory structure and puts in the things you need. By the way, Python commands etc. only have symbolic links. If you want to copy the real thing, use the --always-copy option.

# $VIRTUAL_ENV is a directory created by the virtualenv command
$VIRTUAL_ENV/bin/              # Python,Executable files such as pip
          |-/include/          #C header is placed
          |-/lib/              #Additional libraries will be placed
              |-/pythonX.X/site-package 

Environment variables set by activate script

Just add $ VIRTUAL_ENV / bin to $ PATH. Even if you execute python as $ VIRTUAL_ENV / bin / python, it seems that you can execute Python in the virtual environment without any problem.

activate


#Excerpt from activate script
PATH="$VIRTUAL_ENV/bin:$PATH"
unset PYTHONHOME

Summary

The mechanism of shim in pyenv is a little interesting. What virtualenv does isn't that complicated. If you want to use the version installed in pyenv, you can do as follows.

$ virtualenv path-to-desired-env --python=$HOME/.pyenv/versions/2.7.8/

Recommended Posts

Mechanism of pyenv and virtualenv
python development environment -use of pyenv and virtualenv-
Coexistence of pyenv and autojump
A story of trying out pyenv, virtualenv and virtualenvwrapper
pyenv + virtualenv + Anaconda
Install pyenv and pyenv-virtualenv
Summary of pyenv usage
Pyenv + virtualenv on Mac
Introduction of Virtualenv wrapper
Problems of liars and honesty
Pre-processing and post-processing of pytest
Combination of recursion and generator
Combination of anyenv and direnv
Explanation and implementation of SocialFoceModel
Notes on pyenv and Atom
Differentiation of sort and generalization of sort
Introduction of caffe using pyenv
Use and integration of "Shodan"
Problems of liars and honesty
python with pyenv and venv
Catalina on Mac and pyenv
Occurrence and resolution of tensorflow.python.framework.errors_impl.FailedPreconditionError
[Python] Anaconda, pyenv, virtualenv, .bash_profile
Comparison of Apex and Lamvery
Source installation and installation of Python
Introduction and tips of mlflow.Tracking
Play with the password mechanism of GitHub Webhook and Python
[Required subject DI] Implement and understand the mechanism of DI with Go
Installation procedure + usage of "virtualenv" and "pythonz" in Mountain Lion environment
Environment construction of python and opencv
Various of Tweepy. Ma ♡ and ♡ me ♡
Basic knowledge of Linux and basic commands
The story of Python and the story of NaN
Explanation and implementation of PRML Chapter 4
Raise the version of pyenv itself
Introduction and Implementation of JoCoR-Loss (CVPR2020)
Benefits and examples of using RabbitMq
Explanation and implementation of ESIM algorithm
Danger of mixing! ndarray and matrix
Installation of SciPy and matplotlib (Python)
Significance of machine learning and mini-batch learning
Introduction and implementation of activation function
Memorandum of saving and loading model
I investigated the mechanism of flask-login!
pyenv-change the python version of virtualenv
Misunderstandings and interpretations of Luigi's dependencies
Explanation and implementation of simple perceptron
Environment construction memo of pyenv + conda
Calculation of homebrew class and existing class
This and that of python properties
Design of experiments and combinatorial optimization
Installation and easy usage of pytest
Clash of Clans and image analysis (3)
Install and configure `pyenv` Ansible playbook
Features of symbolic and hard links
Coexistence of Python2 and 3 with CircleCI (1.0)
Summary of Python indexes and slices
Aggregation and visualization of accumulated numbers
Reputation of Python books and reference books
Environment construction with pyenv and pyenv-virtualenv
Use pydoc with Virtualenv / pyenv / venv