My Python Development Environment Setup

I’ve just started learning programming in Python and the famous framework Django and my aim is to become a backend developer. After stumbling for quite a while on the internet, I come up with this seemingly working setup, so I want to write it down for future reference and hope that it may help some others.

Python Version Management

In order to use different versions of Python, there must be a version management tool for easily managing different versions on the same machine, and that is pyenv. It allows the user to install multiple versions of Python and easily switching among them, so the user can test their apps in different environments.

Python Virtual Environment Management

Another useful tool that every Python developer is suggested to use is virtualenv. It’s different from pyenv. pyenv allows you to have mulple versions of Python side by side, say, both version 2.7.14 and version 3.6.3. The default installation location of pyenv is ~/.pyenv/versions/ , so you will see two folders 2.7.14  and 3.6.3  inside that location. virtualenv allows you to specify which Python version to use in a specific folder. If I have three project folders Todo, Music, News in my home directory and I want to use Python 2.7.14 in the project Music, and Python 3.6.3 in the other two project, that’s made possible by virtualenv.

I have two different development environments, one is a MacBook Pro, the other is a Ubuntu running as a virtual machine on the Mac host. The procedures to setup the two environment are slightly different, so I’ll explain them separately.

Mac

My idea is, I may need some general tool for day-to-day tasks that may or may not be related to Python development and it may come as a Python package, so I want a system wide Python installation for those tools. It’s virtualenv in this situation. The operating system running on my Mac is 10.13.1, which come with Python 2.7.10 installed by default. So I installed Python 3.6.3 (the latest version) with Homwbrew:

This installation has pip3 with it, so I can install virtualenv by

pyenv is already in Homebrew repository, so I can install it with

pyenv suggests adding

to shell config file, which is ~/.bash_profile on Macs. But I think I’ll use virtualenv to switch to another Python version and I’ll use the system wide Python without shim interfering, so I use some alias instead to manually enable and disable pyenv/shims. I have these lines in my ~/.bash_profile:

When I want pyenv, I can type enpyenv to enable pyenv and dispyenv to disable pyenv then I don’t. In order to compile Python, some packages are needed prior to installing Python with pyenv, which are readline and xz for my machine, so brew install readline xz to install them. To install the two versions issue pyenv install 2.7.14 and pyenv install 3.6.3 respectively.

Next come into Music folder and type

which will create a folder call venv, which is a Python virtual environment. That means, when it is activated, command python --version always gives  Python 2.7.14. This folder contains all the packages you install in this environment and isolates them from other projects. For instance, if I’m in this environment and I install a package numpy pip install numpy, you can see a folder named numpy appearing in venv/lib/python2.7/site-packages, which I think is the default installation location.

You issue source venv/bin/activate command to activate the virtual environment, but I have alias sv="source venv/bin/activate" in my bash config file so I can just use sv to save some typing. I learned this idea from online, but I cannot find the link I visited.

Then move to Todo and News folder and create Python 3.6.3 virtual environment with virtualenv -p ~/.pyenv/versions/3.6.3/bin/python venv.

Ubuntu

The idea is similar, but Ubuntu (version 16.04 LTS that I’m using) has Python 2.7.12 and Python 3.5.2 pre-installed, but pip3 is not included in the Python3 installation, so to install pip3 just issue sudo apt install python3-pip. pyenv is not in Ubuntu’s official repository, so I use pyenv installer instead. curl and git must be installed first in order to use the installer. Also install required packages for virtualenv to work, as advised here.

Conclusion

Setting up the environment is not as easy, so I hope I can learn Python well that the time I have spent won’t be wasted.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.