Skip to content

Instantly share code, notes, and snippets.

@boundsj
Last active February 6, 2016 13:07
Show Gist options
  • Save boundsj/6309339 to your computer and use it in GitHub Desktop.
Save boundsj/6309339 to your computer and use it in GitHub Desktop.
Setting up a Python environment: Mac

Creating an Agile Python Environment: Mac

This gist assumes you are on a Mac. It also assumes that you already have Python and pip installed and that you are most likely using Python version 2.7.x. If your environment differs you may have to work a little harder to get things sorted but hopefully the gist of the gist (agile Python) will still be useful for you. If you are on a windows machine, this is probably not the gist you are looking for.

We will take the minimum amount of steps nessessary to set up a complete set of tools that are fully controllable from the command line. Use your text editor or IDE of choice for creating the spec and implementation files. Note that some IDEs (i.e. PyCharm) make some of what we cover below a lot easier (i.e. debugging) but it is probably still useful to understand what is going on behind the scenes.

We will create a simple application that pulls down Guido van Rossum's twitter feed and prints out some of his latest perls of wisdom. In the course of completing this exercise we will establish a pattern for creating a python workspace that:

  • Allows you to easily install helpful libraries to speed up development and yet...
  • Is isolated, locked down, and won't change (until you are ready to change it)
  • Allows you to easily target a specific version of Python
  • Promotes test driving your program's implementation
  • Allows you to create mocks of dependencies external to your program so that you can test your interactions with external services in a controlled manner
  • Allows you to debug your code when things go awry

####

••• Simplicity--the art of maximizing the amount of work NOT done--is essential. •••

The virtualenv package (similar to rvm) allows you to create siloed python environments with versioned package installations that are not affected by the main, system packages. The virtualenvwrapper project is essentially a set of shell scripts that makes working with virtualenv even better. Finally virtualenv-burrito wraps up all the goodness into one, easy to install package. Very tasty.

To install, have a look at the burrito repo and then simply run:

$ curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | $SHELL

We'll cover the basic commands in this gist. For a full reference, check out the documentation in the virtualenv and virtualenvwrapper projects. Also, if you are coming from ruby then you might find this post useful.

####

••• Build projects around motivated individuals. Give them the environment and support they need, and trust them to get the job done. •••

Now, let's create a virtual environment and install a testing library that will allow us to write our first spec and drive out the initial behavior of our application. We are going to use pytest. There are several other popular (and less popular) testing frameworks for Python and you should feel free to experiment with them. However, as a quick interweb search will tell you, pytest is a well supported, straight forward framework and a great place to start. It's not BDD, but that is another story. Onward:

First create a new virtual environment by issuing the virtualenvwrapper command:

$ mkvirtualenv guido

You've just created an isolated development environment! This is great becuase the libraries and the Python version in this virtual environment will stay the same even if you install different versions of the libraries and Python on your system. You can double check which Python is currently pointed to by running 'which python' and noting the response:

$ which python                               # what python am I using?
/Users/boundsj/.venvburrito/guido/bin/python # you are using the one in the virtual env

Now let's install our first library, pytest, with pip:

$ pip install pytest

You can check that is worked by inspecting the version:

$ py.test --version                                                       # what pytest have I?
$ py.test v 2.3.5, from ~/.venvburrito/guido/lib/site-packages/pytest.pyc # you have the right one

####

••• Working software is the primary measure of progress. •••

Our environment is already in really good shape. We are already capable of installing any package we want, writing and running tests, and debugging. So, let's do that!

Thanks to the magic of pip packages our guido twitter feed reader will be really short. Let's drive out the initial functionality by creating a spec file called test_guido.py and writing a test that asserts that there is a class called Guido with a method called get_tweets that returns "I am guido therefore I tweet":

Note: Your virtualenv probably (depending on if you customized anything) lives in a hidden directory in your home directory, it should still be invoked since you've already run "mkvirtualenv". If you have closed your shell and are coming back to this exercise, you'll need to run:

$ workon guido

to re-source the virtualenv. You'll need to do this everytime you want to work on guido and are opening a new shell window.

guido_test.py

%%%% % inital test goes here $$$$$$$

Now let's get serious, we are going to install another library called python-twitter that will help us pull down Guido's tweets:

$ pip install python-twitter
##### Special note regarding dependency managment: Since we've installed two libraries (pytest and python-twitter), now would probably be a good time to talk about locking down our library dependencies. To do this we can tell pip to freeze our library and version information into a requirements.txt file:
$ pip freeze > requirements.txt

Now, we can copy our code to a new environment (virtual or otherwise) and install all the dependencies with:

$ pip install -r requirements.txt

It's no "Gemfile.lock" but it'll work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment