Monday, September 24, 2012

roll your own gurobi shell

Note: These instructions have been updated to use the from the Gurobi distribution.

I've recently started using Gurobi for a class. While I'm pleased with the level of Python support they provide, I'm not a fan of the Gurobi shell they ship it with.

  • The shell is Python, which is good. However, instead of bundling the Gurobi libraries as a Python module that I can install alongside my system's native Python, they ship their own Python binaries. If I want to use any third-party modules I have to make them available to Gurobi somehow.
  • They appear to have removed distutils from the Python they ship.
  • The Gurobi shell doesn't give me module docs, tab completion, or anything I'd get from either ipython or bpython. It's nothing more than the rudimentary cPython shell.

They do, however, provide a script, so we don't have to use or the Python that's in the distribution. Here are simple instructions for using bpython as your shell. You should have Gurobi 5.0.1 installed and the GUROBI_HOME environment variable set. (I assume you are installing it into your home directory.)

$ mkdir -p $HOME/lib/python2.7/site-packages/
$ PYTHONPATH=$HOME/lib/python2.7/site-packages/ python install --prefix=$HOME

Update your environment's PYTHONPATH and LD_LIBRARY_PATH:

$ echo "export PYTHONPATH=\$PYTHONPATH:\$HOME/lib/python2.7/site-packages/" >> $HOME/.bashrc
$ echo "export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\$GUROBI_HOME/lib/" >> $HOME/.bashrc
$ source $HOME/.bashrc

Install bpython:

$ sudo apt-get install bpython

Now type bpython and you should have the lovely interface you see below, complete with tab completion and module docs. Isn't that better?


  1. You may want to read 'The Gurobi Python Interface for Python Users', here...

    It gives instructions for using the '' file we provide to do what you did above in a simpler way.

    - Ed

    1. Ah -- perfect. I will update the post.

    2. This should require only two steps (assuming you already have Gurobi installed)...

      > cd /opt/gurobi501/linux64
      > python install

      I'd like to better understand one point...

      "...instead of bundling the Gurobi libraries as a Python module that I can install alongside my system's native Python..."

      We thought that this was what we were doing with ''. Is there something we could have done to make it easier to install the gurobipy module in the system's Python? (BTW, we actually do use the native Python on Mac, but not on Linux. Python isn't really native on Linux - it is an optional package in most distributions.)