Next: Simple Python Example Up: Gurobi Optimizer Quick Start Previous: Building and running the
The Gurobi Python interface can be used in a number of ways. It is the basis of our Interactive Shell, where it is typically used to work with existing models. It can also be used to write standalone programs that create and solve models, in much the same way that you would use our other language interfaces. The Gurobi distribution includes a Python interpreter and a basic set of Python modules. If you'd like additional capabilities, you can also install the Anaconda Python distribution, which includes both a graphical development environment (Spyder) and a notebook-style interface (Jupyter).
When comparing our Python interface against our other language interfaces, you will find that our Python interface adds a few higher-level constructs that allow you to build models using a more mathematical syntax, similar to the way you might work with a traditional modeling language. This section will work through two Python modeling examples. The first will present a Python program that is similar to the C, C++, Java, and C# programs presented in previous sections. The second demonstrates some of the higher-level modeling capabilities of our Python interface.
This section assumes that you are already familiar with the Python programming language, and that you have read the preceding section on the Gurobi Interactive Shell. If you would like to learn more about the Python language, we recommend that you visit the Python online tutorial.
As we noted, the Gurobi distribution includes all the tools you will
need to run Python programs. However, if you would prefer to use your
own Python installation, we also provide tools for installing the
gurobipy module into your Python environment. You should refer
to the instructions for building and running the
for further details.
One big advantage of working within Python is that the Python language is popular and well supported. One aspect of this support is the breadth of powerful Python Integrated Development Environments (IDEs) that are available, most of which can be downloaded for free from the internet. This document includes instructions for setting up Gurobi for use within the Anaconda distribution. Anaconda greatly simplifies the task of installing Python packages, and it includes both a graphical development environment (Spyder) and a notebook-style interface (Jupyter). If you plan to do significant Python development, we recommend that you install Anaconda now. You will also find pointers to other useful Python tools there.
Important note for AIX users: due to limited Python support on AIX, our AIX port does not include the Interactive Shell or the Python interface. You can use the C, C++, or Java interfaces.
The Python example directory contains a number of examples. We encourage you to browse and modify them in order to become more familiar with the Gurobi Python interface. We also encourage you to read the Gurobi Example Tour for more information.
- Simple Python Example
- Example mip1.py
- Example details
- Creating the model
- Adding variables to the model
- Setting the objective
- Adding constraints to the model
- Optimizing the model
- Reporting results - attributes
- Error handling
- Running the example
- Python Dictionary Example
- Lists and Tuples
- List comprehension and generator expressions
- The tuplelist class
- The tupledict class
- netflow.py example
- netflow.py example details
- Building a multi-dimensional array of variables
- Arc capacity constraints
- Flow conservation constraints
- Building and running the examples