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.
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 runtime version so that found pyenv runtime version manager.
Pyenv is a simple, powerful and cross-platform tool for managing multiple versions of Python on a single machine. It is very useful when you need to switch between different Python versions for different projects, or when different applications on your system require different Python versions.
So, we can install, uninstall, list or switch between different versions.
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 post.
Run the following commands in your terminal. But, just copy commands without $ sign.
$ brew update
$ brew install pyenv
Setting PATH
Checking shell
output of echo $SHELL
can be bash
, zsh
or fish
$ echo $SHELL
/usr/local/bin/bash
# OR
/bin/zsh
Run the following command for bash, zsh, etc configuration.
$ echo 'eval "$(pyenv init -)"' >> ~/.zshrc
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
.
.
.
.
.
.
Filter Python versions with grep
Way 1
This command will list all available Python versions starting with '3.' The ^[[:space:]]*3\.
pattern matches lines that start with any number of spaces, followed by '3.'
$ pyenv install --list | grep '^[[:space:]]*3\.'
Way 2
We can use grep
with tr
to trim the leading spaces. The tr -d " "
command deletes (-d
) all spaces from the input. Then grep
can find lines starting with '3.' without having to worry about leading spaces.
$ pyenv install -l | tr -d " " | grep '^3\.'
Filter Python versions with awk
This command lists all available Python versions starting with '3.'. The /^ *3\./
pattern matches lines that start with any number of spaces (indicated by the *
after the space), followed by '3.'.
$ pyenv install -l | awk '/^ *3\./'
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
$ pyenv which pip
/Users/kenanhancer/.pyenv/versions/test31-3.9.17/bin/pip
$ 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 the standard package manager for Python packages, or modules like npm(Node Package Manager in Node.js community)
With pip
, you can install packages from the Python Package Index (PyPI) and other indexes. PyPI is a repository of software for the Python programming language and hosts thousands of third-party modules that make Python a more powerful language.
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.