Installing pyenv on Unix/Linux

pyenv is a tool that allows you to easily switch between multiple versions of Python installed on the host. This is very useful when developing software, when there are several different very and they need support.

Installing pyenv on Unix/Linux

pyenv is a great tool for managing multiple Python versions. Even if you have already installed Python in your system, you should install pyenv, so you can easily try out new language features or help to contribute to a project that is on a different version of Python.

Recommended reading:

Installing virtualenv + virtualenvwrapper in Unix/Linux

So let’s start the installation…

Installing pyenv on CentOS/Fedora/RedHat

Plug-in EPEL repository:

Install EPEL

Next, you should install the following packages:

# yum install zlib-devel bzip2 bzip2-devel readline-devel sqlite 
sqlite-devel openssl-devel xz xz-devel libffi-devel

A simple script to install this SOFTWARE, you can use:

$ cd /usr/local/src && curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

After installation, you should configure and then you can use this Tula.

Installing pyenv on Debian/Ubuntu

You should install the following packages:

# apt-get install -y make build-essential libssl-dev zlib1g-dev 
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev 
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl

A simple script to install this SOFTWARE, you can use:

$ cd /usr/local/src && curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

After installation, you should configure and then you can use this Tula.

To install pyenv in MacOS

Put homebrew, the article can be taken here:

Install homebrew on Mac OS X

Let’s search for packages:

$ brew search pyenv

To put this IN the running:

$ brew install pyenv

Let’s tune.

Setup pyenv in Unix/Linux

Open the file:

$ vim ~/.bashrc

Prescription:

export PYENV_ROOT="${HOME}/.pyenv"

if [ -d "${PYENV_ROOT}" ]; then
 export PATH="${PYENV_ROOT}/bin:${PATH}"
 eval "$(pyenv init -)"
 eval "$(pyenv virtualenv-init -)"
fi

Just add this code to the end of your ~/ .bashrc and then download your profile to download these add-ons:

For the RPM’s OS:

$ source ~/.bash_profile

Or:

$ . ~/.bash_profile

For DEB’s OS:

$ source ~/.profile

For MacOS:

$ source ~/.bash_profile

You can already using!

Using pyenv on Unix/Linux

In order to find out which version of python is being used at the moment, there is a command:

$ pyenv install-l

To install any version, use:

$ pyenv install 2.7.16 && 
 pyenv install 3.5.7

Look what is available in the system:

$ pyenv versions
* system
2.7.16
 3.5.7

To switch to the required version, use:

$ pyenv local 2.7.16

Also, you can use:

$ pyenv local some_project

I’m going to use:

$ pyenv local 2.7.16 3.5.7

For global use, you can put also need(s) version(s):

$ pyenv global 3.5.7 2.7.16

You can set a specific version of Python for shell shell, for example:

$ pyenv shell 3.5.7

How it looks in the picture:

Create the sandbox:

$ pyenv virtualenv -p python2.7 2.7.16 my_project-with-2.7.16

Or:

$ pyenv virtualenv 2.7.16 my_project-with-2.7.16

Look at the sandbox:

$ pyenv virtualenvs

Now, I will put my desired version:

$ pyenv local my_project-with-2.7.16 3.5.7

Look version of Python:

$ python -V
Python 2.7.16

Look where the Python:

$ which python
/Users/captain/.pyenv/shims/python

And:

$ pyenv which python
/Users/captain/.pyenv/versions/my_project-with-2.7.16/bin/python

For a list of commands mnozhno use:

$ pyenv commands
--version
activate
commands
completions
deactivate
exec
global
help
hooks
init
install
local
prefix
realpath.dylib
rehash
root
shell
shims
uninstall
version
version-file
version-file-read
version-file-write
version-name
version-origin
versions
virtualenv
virtualenv-delete
virtualenv-init
virtualenv-prefix
virtualenvs
virtualenvwrapper
virtualenvwrapper_lazy
whence
which

To help, you can:

$ pyenv --help
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
 commands List all available pyenv commands
 local Set or show the local application-specific Python version
 global Set or show the global Python version
 shell Set or show the shell-specific Python version
 install Install a Python version using python-build
 uninstall Uninstall a specific Python version
 rehash Rehash pyenv shims (run this after installing executables)
 version Show the current Python version and its origin
 versions List all Python versions available to pyenv
 which Display the full path to an executable
 whence List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

Or, for a specific command:

$ pyenv activate --help
Usage: pyenv activate the <virtualenv>
 pyenv activate --unset

Activate a Python virtualenv environment in current shell.
This acts almost as same as `pyenv shell`, but this invokes the `activate`
script in your shell.

<virtualenv> should be a string matching a Python version known to pyenv.

Ie to activate the created environment, use:

$ pyenv activate my_project-with-2.7.16

To disable, the following command:

$ pyenv deactivate

To delete a version of Python with OS, use:

$ pyenv uninstall 2.7.16

To update pyenv, there conmand:

$ pyenv update

To remove, use:

$ rm-rf $PYENV_ROOT

NOTE: By default, $PYENV_ROOT — ~/.pyenv.

Also, you should remove the following lines from ~/.bashrc:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

That’s all, “Install pyenv on Unix/Linux” is completed.

Source: linux-notes.org

(Visited 194 times, 2 visits today)