Hi, there! This is my first time blogging and I’ve decided to talk about Python tools, which I’ve been using lately. If you have problems dealing with multiple python and pip versions, then this might help you. Notice that when I say multiple versions, it doesn’t mean that you need to have a bunch of them to fit in this case. I currently use versions 2.7 and 3.4.

The hard way

A very common error that shows up many times while installing a module is a lack of permission that you may bypass using sudo, but you are probably gonna miss this on your first try, as follows:

vagrant@vagrant-ubuntu-trusty-64:~$ pip install Flasky
# some outputs while installing and suddenly...
error: could not create '/usr/local/lib/python2.7/dist-packages/flasky': Permission denied

----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_vagrant/Flasky/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-VMeR0c-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_vagrant/Flasky
Storing debug log for failure in /home/vagrant/.pip/pip.log

The example below used to happen often to me. Because it’s common to type pip to install a module I was tricked by myself once I used python3 to run the scripts. The thing is that I should have used pip3 to install a module to my python3 distribution. It may sound obvious, but I see it happening a lot with other people.

vagrant@vagrant-ubuntu-trusty-64:~$ sudo pip install Flask
# some outputs and warning while installing...
Successfully installed Flask Werkzeug Jinja2 itsdangerous MarkupSafe
Cleaning up...
vagrant@vagrant-ubuntu-trusty-64:~$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import flask
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'flask'
>>>

Another point that gets annoying happens when trying to install another version of python, which doesn’t come by default. As I’m using a vagrant box with Ubuntu Trusty64, that comes with python 2.7 and 3.4, I already have what I need.

Suppose we’re installing Python 3.5. Although I’m not showing this one here, there are many chances for you to end up overwritting your python3 binary, or you will have to deal with symlinks to handle both versions. Check out this link. If you decide to take a look at the python docs, you’ll be asked to go to other links. Be careful not to override your python3 binary (see the image bellow). You can also follow the steps above on YouTube. YouTube.

warning I took it from this python doc page

A cleaner path

There is a way of freeing yourself from that hard road. A tool called pyenv gets things really easy. Thanks, Yuu Yamashita. Its purpose of life is to manage python versions. You can seamlessly switch between versions and also install new versions of python, which incentivizes you trying new stuff since you have no headache, or at least almost no headache.

It gets you rid of those permission issues. Pyenv concentrates all python versions in a folder on your home directory, so you are supposed to have full access. Also, you don’t need to care about which pip version you are installing a new module, nor type python<VERSION>. Once you choose which version of python you want to use, the tool proxies everything you need.

I want to install a python module to my python 3.4.3, so I check my current version and the ones I had installed:

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
  3.4.3

I change it to 3.4.3:

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv global 3.4.3
vagrant@vagrant-ubuntu-trusty-64:~$ pyenv versions
  system
* 3.4.3 (set by /home/vagrant/.pyenv/version)

And from now on, my python commands are being tunneled to the correct version.

If I want to install 3.5 version:

vagrant@vagrant-ubuntu-trusty-64:~$ pyenv install 3.5.0
Downloading Python-3.5.0.tgz...
Installing Python-3.5.0...
Installed Python-3.5.0 to /home/vagrant/.pyenv/versions/3.5.0

Installation

For Linux, you can follow pyenv-installer, which recommends:

$ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

For Mac, you can use brew:

$ brew update
$ brew install pyenv

To be continued…

Here are some benefits I got after adopting this tool:

  • I get rid of some environment issues like permissions;
  • Wrong pip/python versions when installing new modules;
  • Easy installing of new python version;
  • Actually installing new versions since this became easy;
  • Seamlessly switching between python versions.

If the benefits above fit you, then it is probably worth trying this out. Hope it helps! : )

PS: There is another tool called pyenv-virtualenv that combined with this one make things even better. That’s what Part 2 of this post is gonna be about.