It is a good habit to keep all things in a well-defined order. When I was doing scientific calculation in python, one thing confused me in the beginning is the python version and the corresponding library. Then I found pyenv and virtualenv, which are all good tools to manage python versions and libraries. Now I will write them in detail.
Why pyenv and virtualenv?
One thing to consider is why choosing these two tools.
Pyenv, a wonderful tool to manage different python versions, is located in Github. When we want to have access to both python2 and python3, it is so painful to change frequently in terminal. Although it is possible to install them all and use python in terminal to choose python2 and python3 to choose python3 respectively, it will be in a mess when we are installing packages.
So pyenv defines a way as follows: It set up a directory named .pyenv in your home folder, and install its own python version, say 2.7.8 or 3.4.3, whatever you like. Then it add its path to the global PATH, before the system python path. Then when we type python into the terminal, it will be used instead of the system default one.
Pyenv configures the default version based on the system by default, but you can change the version at your demand in a specific directory. Then inside this particular folder, the version of python will be the one you specified.
Virtualenv is another powerful tool. It will set up a new python environment based on your actual need. Pyenv will setup the fundamental python version, but virtualenv will let you modify it in your own way. Suppose you want to install a package to a specific project, and it will be of no use after this project, then the best way to do so is to configure a new virtual python environment.
How to use them?
Now I will write the commands step by step.
First we need to install both these tools. Here I recommend using Homebrew:
The installation is easy, but we also need to add these two lines to the default bash profile. I use zsh, so add them to .zshrc in the home folder, and don’t forget to source it after edition:
Now the installation is done. Next, we need to check what’s the default pyenv environment:
This command will show the current python version. The output will be system. Then let’s install python 2.7.8 and 3.4.3 respectively:
Now if I run
We will get the result as:
* system (set by /Users/Elliot/.pyenv/version) 2.7.8 3.4.3
which means the python 2.7.8 and 3.4.3 has been successfully installed and the default version is still system.
After Installation, we had better add the versions of python into the shell, for which we use:
If we want to change the default version, we can run this command:
This will change the global default python version to be 3.4.3 in pyenv.
Next let’s setup a new directory,
and if I want to use 2.7.8 here
Now if we run
We can see the default version becomes 2.7.8 only in this folder.
The packages can be installed via pip (for python2) or pip3 (for python3), and the corresponding libraries will be installed in the ~/.pyenv/versions/… directory, specific to each version.
Suppose I make a new project, then I want to create a virtual environment named newPython:
This will create a new python environment named newPython in ~/.pyenv/versions/, and the version is 3.4.3. If the version name is omitted, the system default version will be used.
Then we can activate and deactivate this particular environment:
Please see the result. We can also install packages, but it is only visible in this particular environment.
If we want to uninstall it, we can either delete the folder in ~/.pyenv/versions/, or just run
We can use
to see all the virtualenvs we have created.
Now I can install the python scientific calculation libraries:
All configurations have been done, enjoy it!