Never again depend on system Python
There was a time when you had no choice and had to use system Python to develop. That is not a big deal, UNLESS you want to use a version other than the outdated Python version your OS provides.
There are a lot of reasons why your OS does not update a package, but when developing, it is better to use the exact version needed, since most of the time we don’t have as much control over the production environment as we have in our dev machine.
So you see, it was not a good situation. You would end up with two or more Python’s in your system, and it was very easy to accidentally uninstall the older version in favor of the newest, and find out half of your system stopped working…
When I first heard about Pythonbrew (yes, I meant Pythonbrew rather than Pyenv) I was amazed. Easy to install Python’s and not break anything, nor need super powers for it (you only had to hope the system had all the required packages… more on that later). Good alternative even for production environments.
Unfortunately Pythonbrew was discontinued. But worry not, Pyenv was there to save the day. The author has also several related repositories for extra niceties. For example, to install it, you use pyenv-installer, and so on.
And, you are not limited to CPython. You have tons of flavours to choose from.
So let’s get our hands dirty.
As I just said, there is an installer, and all you need to do is:
$ curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
Now add this to your .bash_profile:
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Installing a Python
Make sure you source your .bashrc or open a new terminal after the previous step to init Pyenv in your shell (of corse, you will need to be sourcing .bash_profile from .bashrc as done previously)
$ . ~/.bashrc
Just not yet
Remember when I said Ubuntu comes with some missing things? Well, I left this part out of the previous article because they are required for compiling Python, and we better not forget those here.
Alright, we can actually compile Python without them, but things will fail later on when you try to use them. And BTW, here is where you will need su privileges, so if installing in production, hope for the best…
We’ll need python-dev libraries, readline, bz2, ssl, sqlite and xml:
$ sudo apt-get install python-dev libreadline-dev libbz2-dev libssl-dev libsqlite3-dev libxslt1-dev libxml2-dev libxslt1-dev
Now we are ready to install anything.
Pick a Python
Let’s stick with latest 2.x and 3.x CPython. A the time of writing, Google App Engine for example has only 2.x support, so we still need it. As for 3.x, Django already supports it, although I don’t have any real projects using it yet, but it is fun to have anyway.
$ pyenv install 2.7.8; pyenv install 3.4.1
Check the installed versions
$ pyenv versions
$ pyenv rehash
Set 2.x as global
$ pyenv global 2.7.8
This is another nicety we have. First of all, besides using Pyenv, you should ALWAYS work inside a virtualenv. I guess I don’t need to tell you why to use virtualenvs. If you don’t know/don’t remember, go look it up, now!
$ git clone https://github.com/yyuu/pyenv-virtualenvwrapper.git ~/.pyenv/plugins/pyenv-virtualenvwrapper
Add this to your .bash_profile:
Source .bashrc and install virtualenvwrapper for the current Python version:
$ . ~/.bashrc
$ pyenv virtualenvwrapper
Now we are finally ready to create virtualenvs using virtualenvwrapper:
$ mkvirtualenv my_project
For the record, so far we have Setup Ubuntu and now installed Python using Pyenv with virtualenvwrapper. Next time we’ll get to setup Pycharm!
Until next time,