The official documentation for entering your CPLEX API key can be found here (you've probably already seen this). This documentation assumes that you have installed the Community Edition (aka, the "free edition") of CPLEX Optimization Studio and you can find a link to download that here. You can do that, if you like, and you should be up and running.
An alternative is to use the undocumented CPLEX_CPXCHECKLIC_BINDIR
environment variable. This is particularly useful for Python users that may not choose to install the rest of CPLEX Optimization Studio. When you installed the cplex
package into your anaconda environment (or for others that installed via pip
), it included an executable named cpxchecklic
. This can be found in the bin
or Scripts
directory of your Python environment. If you set the CPLEX_CPXCHECKLIC_BINDIR
environment variable to the directory that contains cpxchecklic
, and you have already set the CPLEX_STUDIO_KEY
environment variable correctly, then you should be good to go.
If you continue to have problems after following the advice above, the following Python script may come in handy to help with troubleshooting problems you're having with your API key (please update your question with the output if that is the case):
"""Tests Python Download-and-go setup.
usage: python test.py [API_KEY] [CPXCHECKLIC_BINDIR]
The API_KEY and CPXCHECKLIC_BINDIR arguments are optional.
"""
import os
import sys
import cplex
# Column limit for the Community Edition.
CPLEX_COLUMN_LIMIT = 1000
# Print the CPLEX version.
print("Version:", cplex.__version__)
# Allow the user to pass in the API key.
if len(sys.argv) > 1:
os.environ["CPLEX_STUDIO_KEY"] = sys.argv[1]
# Allow the user to pass in the cpxchecklic bindir.
if len(sys.argv) > 2:
os.environ["CPLEX_CPXCHECKLIC_BINDIR"] = sys.argv[2]
# Print the environment variables.
for var in ("CPLEX_STUDIO_KEY",
"CPLEX_STUDIO_DIR1210",
"CPLEX_CPXCHECKLIC_BINDIR"):
print(var, "=", os.getenv(var))
# Create a model that will not work with the Community Edition.
cpx = cplex.Cplex()
cpx.variables.add(lb=[0.0] * (CPLEX_COLUMN_LIMIT + 1))
# Solve the model and print the solution status.
cpx.solve()
print("Status: {0} ({1})".format(cpx.solution.get_status_string(),
cpx.solution.get_status()))