Setting up the Python API of CPLEX

The Python API of CPLEX is part of IBM ILOG CPLEX Optimization Studio.

The modules associated with the CPLEX Python API reside in the directory yourCPLEXhome/python/VERSION/PLATFORM, (or in the folder yourCPLEXhome\python\VERSION\PLATFORM) where yourCPLEXhome specifies the location where CPLEX is installed as part of IBM ILOG CPLEX Optimization Studio, VERSION designates a version of Python compatible with CPLEX, and PLATFORM stands for your combination of operating system and compiler.

There are two alternative ways to set up the Python API of CPLEX.
  • The preferred and most common way is to use the script setup.py located in the directory yourCPLEXhome/python/VERSION/PLATFORM (or in the folder yourCPLEXhome\python\VERSION\PLATFORM).
  • Alternatively, you can set the environment variable PYTHONPATH to yourCPLEXhome/python/VERSION/PLATFORM and start running Python scripts with CPLEX.
Both of these methods are detailed further in the following paragraphs.

Using the script setup.py

To install the CPLEX-Python modules on your system, use the script setup.py located in yourCplexhome/python/VERSION/PLATFORM. If you want to install the CPLEX-Python modules in a nondefault location, use the option --home to identify the installation directory. For example, to install the CPLEX-Python modules in the default location, use the following command from the command line:


python setup.py install

To install in the directory yourPythonPackageshome/cplex, use the following command from the command line:


python setup.py install --home yourPythonPackageshome/cplex

Both of those commands (default and home-specified) invoke the Python package distutils. For other options available with that package, consult the documentation of Python distutils.

Setting the environment variable PYTHONPATH

If you run multiple versions of CPLEX side-by-side, then use this way of declaring the location of CPLEX and its Python API to your Python installation by means of the environment variable PYTHONPATH.

To start using the CPLEX Python API, set the Python path environment variable PYTHONPATH to the value of yourCplexhome/python/VERSION/PLATFORM. Setting this environment variable enables that version of Python to find the CPLEX modules that it needs to run Python commands and scripts that use the CPLEX Python API.

Next steps

After setting up your Python environment by means of one of those alternative methods, you can proceed to the topic Starting the CPLEX Python API.