Zhen Zhang's Blog

Python Environment Configuration

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

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

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:

1
2
brew install pyenv
brew install pyenv-virtualenv

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:

1
2
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Now the installation is done. Next, we need to check what’s the default pyenv environment:

1
pyenv version

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:

1
2
pyenv install 2.7.8
pyenv install 3.4.3

Now if I run

1
pyenv versions

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:

1
pyenv shell 2.7.8 3.4.3

If we want to change the default version, we can run this command:

1
pyenv global 3.4.3

This will change the global default python version to be 3.4.3 in pyenv.

Next let’s setup a new directory,

1
2
mkdir pyenvTest
cd pyenvTest

and if I want to use 2.7.8 here

1
pyenv local 2.7.8

Now if we run

1
pyenv version

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:

1
pyenv virtualenv 3.4.3 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:

1
2
pyenv activate newPython
pyenv deactivate

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

1
pyenv uninstall newPython

We can use

1
pyenv virtualenvs

to see all the virtualenvs we have created.

Now I can install the python scientific calculation libraries:

1
pip3 install numpy scipy matplotlib pandas ipython

All configurations have been done, enjoy it!