Monday, September 24, 2012

roll your own gurobi shell

Note: These instructions have been updated to use the setup.py 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 setup.py script, so we don't have to use gurobi.sh 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/
$ cd $GUROBI_HOME
$ PYTHONPATH=$HOME/lib/python2.7/site-packages/ python setup.py 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?

3 comments :

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

    http://www.gurobi.com/documentation/5.0/quick-start-guide/node5

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

    - Ed


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

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

      > cd /opt/gurobi501/linux64
      > python setup.py 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 'setup.py'. 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.)

      Delete