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.
A tool for using different python versions within the same terminal. You can set the python version to use for each working directory.
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.
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.
Examine the environment variable PYENV_VERSION
and use the version information, if any.
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.
If the .python-version
file does not exist in the same directory, search the parent directory for the .python-version
file.
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.
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
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
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