Python: Life is easier with Pyenv
About 17 years ago I spend about a month learning Python. This was just for personal education and did not involve myself in any Python project. My focus was mainly on Java related projects. About couple of months back, I got an opportunity to work on Python. Bascially I had to restart my learning from scratch. I was very excited to learn anything and everything about Python.
As usual I started from roots by googling various tutorials which included step by step guides. As we know, the fundamentals to learning any programming language effectively is to practice it and for that I needed to have Python on my Macbook Air. By default Mac comes with Python 2.7. Every tutorial that I encountered different versions of Python 2.x or 3.x or sometimes even 3.x.x. When you are new, it is always good to stay with the latest, but many examples are written with older versions.
After installing few versions of Python and the complexities of switching between versions, I came across a wonderful tool called “pyenv”. Believe me, this is a life-saver!
The examples here are with regard to Mac. See Pyenv to track more real-time updates.
1. First, check if you already have pyenv installed
$ pyenv
…..If you see -bash: pyenv: command not found, then it means you do not have it.
2. Install pyenv
$ brew install pyenv
Modify the following to your .bash_profile
eval “$(pyenv init -)”
3. Verify installation
$ pyenv
pyenv 1.2.13
Usage: pyenv <command> [<args>]Some useful pyenv commands are:
…..
4. Next, install a specific Python version.
First, check the list of versions available
$ pyenv install — list
Available versions:
2.1.3
2.2.3
2.3.7
2.4.0
2.4.1
…..and so on
Then, install the version of your choice
pyenv install 3.6.8
After the installation, check the list of versions.
$ pyenv versions
system
2.7.10
*3.6.8 (set by /Users/janedoe/Development/testproj/.python-version)
3.6.9
3.7.4…..
You can see that I have few Python versions installed on my computer and the active version is 3.6.8.
5. Switching between Python versions
This is the last and most fun step! In the step above, you saw that I have Python version 3.6.8 installed. If I want to change to another version, then it is as simple as….
$ pyenv global 3.6.9
$ pyenv versions
system
2.7.10
3.6.8
* 3.6.9 (set by /Users/michael/.pyenv/version)
3.7.4
You can see how I switched from Python 3.6.8 to 3.6.9
Well, good luck. I hope you will have fun with Python. Don’t hesitate to send me a message if you have questions.
