How to install Python with pyenv runtime version manager

I am using different programming languages like C#, Java, Node.js etc and need to manage runtime versions in my local. So, I like to use runtime version managers.

For example, there are some runtime version managers in Node.js community like nvm or n. I am very happy to use both of two package managers 🙂

Let's say I work for one Node.js project in dev machine with Node v8.0.0 but other project needs Node v6.0.0

In order to handle, different versions in same machine, we may use runtime version managers.

I just want to find a way to switch between different Python version so that found pyenv runtime version manager.

Pyenv installation

Click pyenv link for more detailed installation.

Installing pyenv with Homebrew

First, ensure that you have Homebrew installed in your system. Homebrew is a package manager for Mac OS. So if you want to learn more details about it, visit my blog below.

Run the following commands in your terminal. But, just copy commands without $ sign.

$ brew update
$ brew install pyenv

Setting PATH

Run the following command for advanced configuration.

$ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Run the following command for basic configuration.

$ echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.bash_profile

and restart your terminal manually or run the following command to restart automatically.

$ exec "$SHELL"

Upgrading pyenv

$ brew upgrade pyenv

Uninstall pyenv

$ rm -rf $(pyenv root)
$ brew uninstall pyenv

Pyenv commands

List all Python versions installed in system

$ pyenv versions

  system
  3.7.3
* 3.8.2 (set by /Users/kenanhancer/.pyenv/version)

Show current active Python version in system

$ pyenv version

3.8.2 (set by /Users/kenanhancer/.pyenv/version)

List all available Python versions

Pyenv will show very long list but I just took some part of it. (still many part of list is here 🙂 )

$ brew update && brew upgrade pyenv
$ pyenv install --list

Available versions:
  2.1.3
  2.2.3
  2.3.7
  2.4.0
  2.4.1
  2.4.2
  2.4.3
  2.4.4
  2.4.5
  2.4.6
  2.5.0
  2.5.1
  2.5.2
  2.5.3
  2.5.4
  2.5.5
  2.5.6
  2.6.6
  2.6.7
  2.6.8
  2.6.9
  2.7.0
  2.7-dev
  2.7.1
  2.7.2
  2.7.3
  2.7.4
  2.7.5
  2.7.6
  2.7.7
  2.7.8
  2.7.9
  2.7.10
  2.7.11
  2.7.12
  2.7.13
  2.7.14
  2.7.15
  2.7.16
  2.7.17
  3.0.1
  3.1.0
  3.1.1
  3.1.2
  3.1.3
  3.1.4
  3.1.5
  3.2.0
  3.2.1
  3.2.2
  3.2.3
  3.2.4
  3.2.5
  3.2.6
  3.3.0
  3.3.1
  3.3.2
  3.3.3
  3.3.4
  3.3.5
  3.3.6
  3.3.7
  3.4.0
  3.4-dev
  3.4.1
  3.4.2
  3.4.3
  3.4.4
  3.4.5
  3.4.6
  3.4.7
  3.4.8
  3.4.9
  3.4.10
  3.5.0
  3.5-dev
  3.5.1
  3.5.2
  3.5.3
  3.5.4
  3.5.5
  3.5.6
  3.5.7
  3.5.8
  3.5.9
  3.6.0
  3.6-dev
  3.6.1
  3.6.2
  3.6.3
  3.6.4
  3.6.5
  3.6.6
  3.6.7
  3.6.8
  3.6.9
  3.6.10
  3.7.0
  3.7-dev
  3.7.1
  3.7.2
  3.7.3
  3.7.4
  3.7.5
  3.7.6
  3.7.7
  3.8.0
  3.8-dev
  3.8.1
  3.8.2
  3.9.0a5
  3.9-dev
  activepython-2.7.14
  activepython-3.5.4
  activepython-3.6.0
  anaconda-1.4.0
  anaconda-1.5.0
  anaconda-1.5.1
  anaconda-1.6.0
  anaconda-1.6.1
.
.
.
.
.
.

Installing Python versions

$ pyenv install 3.7.3

$ pyenv install 3.8.2

Setting global default version in your system

$ pyenv global 3.7.3

Go back to the system version of Python as the default

If you want to go back to your system default Python version, run the following code.

$ pyenv global system

Switch between Python versions

$ pyenv shell 3.7.3
$ pyenv local 3.7.5
$ pyenv global 3.10.4

Checking Python version

$ pyenv version

3.7.3 (set by /Users/kenanhancer/.pyenv/version)

or

$ python --version

Python 3.7.3

Checking Python path

$ pyenv which python

/Users/kenanhancer/.pyenv/versions/3.7.3/bin/python
$ which python

/Users/kenanhancer/.pyenv/shims/python

Checking Pip version

$ pip --version

pip 19.0.3 from /Users/kenanhancer/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)

Checking Pip path

$ which pip

/Users/kenanhancer/.pyenv/shims/pip

Updating PIP

$ pip install --upgrade pip

Requirement already up-to-date: pip in ./.pyenv/versions/3.8.2/lib/python3.8/site-packages (20.1)

What is PIP?

PIP is a package manager for Python packages, or modules like npm(Node Package Manager in Node.js community)

A package contains all the files you need for a module.

Modules are Python code libraries you can include in your project.

If you have Python version 3.4 or later, PIP is included by default.

Leave a Reply