IBMDecisionOptimization / docplex-examples

These samples demonstrate how to use the DOcplex library to model and solve optimization problems.
https://ibmdecisionoptimization.github.io/
Apache License 2.0
396 stars 229 forks source link

module cplex has no attribute cplex #34

Closed bhimabi closed 3 years ago

bhimabi commented 4 years ago

I am currently creating a model in pyomo and using cplex as a solver(persistant cplex) to be specific which uses the python API.

I tried running this on my local windows machine and after installing setup.py it work. However, when i switch to run this on my linux server, I keep getting the error "Module cplex has no attribute CPLEX". It is also asking to checking if I have gotten my python bindings right.

CPLEX : v12.10 Python : v3.6 (64 bit)

I have tried creating virtual environments and adding the original cplex installation location(/opt/...) to my PATH but to no avail.

Let me know if you need any additioanl information and any help would be apprecaited

vlkong commented 4 years ago

Hello,

In order to provide support for you, I need to know what you executed exactly.

Can you please give me more context about I keep getting the error "Module cplex has no attribute CPLEX" ? What are you running to get that error ? If it is your code, please include the code fragment that raises that error ? I also need you to check your installation, for instance by running pip freeze | grep cplex

You also mentionned:

I have tried creating virtual environments and adding the original cplex installation location(/opt/...) to my PATH but to no avail.

What exactly did you do ? Adding things to your PATH does not really help python programs ?

Thanks

bhimabi commented 4 years ago

Sorry about the incomplete information.

I ran the pip freeze command and it returned nothing, which means pip is not able to find CPLEX.

I am creating a model on pyomo and trying to run it using cplex_persistant. I have left of the model as I have run it on pyomo and it works fine on my local windows machine. The error comes when running the final solve line.

    SolverFactory.register('cplex_persistent', doc='Persistent python interface to CPLEX')
    Model = ConcreteModel()
    ...
    X = CPLEXPersistent(model=Model)
    X.options['threads'] = 1
    X.options['mip_limits_solutions'] = 20
    X.options['timelimit'] = 1
    X.options['emphasis_mip'] = 1
    X.solve(report_timing=True, tee=True, warmstart=True)

When I run the solve command, I get the following long error. I can't recall if the first line of the error('import of cplex failed') was there before I ran the pip freeze command.

Import of cplex failed - cplex message=module 'cplex' has no attribute 'Cplex'

Traceback (most recent call last):
  File "/home/asubbara/env/lib64/python3.6/site-packages/pyomo/solvers/plugins/solvers/cplex_direct.py", line 334, in _set_instance
    self._solver_model = self._cplex.Cplex()
AttributeError: module 'cplex' has no attribute 'Cplex'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "Castro MILP.py", line 315, in <module>
    X = CPLEXPersistent(model=Model)
  File "/home/asubbara/env/lib64/python3.6/site-packages/pyomo/solvers/plugins/solvers/cplex_persistent.py", line 51, in __init__
    self.set_instance(self._pyomo_model, **kwds)
  File "/home/asubbara/env/lib64/python3.6/site-packages/pyomo/solvers/plugins/solvers/persistent_solver.py", line 86, in set_instance
    return self._set_instance(model, kwds)
  File "/home/asubbara/env/lib64/python3.6/site-packages/pyomo/solvers/plugins/solvers/cplex_direct.py", line 341, in _set_instance
    raise Exception(msg)
Exception: Unable to create CPLEX model. Have you installed the Python bindings for CPLEX?

        Error message: module 'cplex' has no attribute 'Cplex'

This is what i have tried/done so far:

  1. Installed cplex using 'pip install cplex' -Did not work as the free version has limits on size and i had access to the educational version -This got the closest to working but was sure how to import the educational license into the free version's installation

  2. Removed cplex installed in previous section -Installed cplex using setup.py as mentioned in ibm's website -I used the site-packages location of the virtual environement as my home when installing it -This worked on my windows machine but isnt on the linux server https://www.ibm.com/support/knowledgecenter/en/SSSA5P_12.8.0/ilog.odms.cplex.help/CPLEX/GettingStarted/topics/set_up/Python_setup.html

  3. Tried to add the directory to the main cplex installation and that of the installation from setup.py to PATH to see if that helps python find the CPLEX isntallation

I hope this helps. I suspect that either I installed cplex wrongly or python is not able to find the installation of CPLEX. I've tried everything i can think of but to no avail.

Thanks for the help in advance

vlkong commented 4 years ago

Hi,

So you have your setup on your machine OK, now you want to be able to setup your linux server.

in your point (2), there are some unclear points. 1 . -I used the site-packages location of the virtual environement as my home when installing it

what do you mean by "used the site-packges location of the virtual env? What are you using? anaconda or virtual env ?

  1. you say "This worked on my windows machine but isnt on the linux server" - Can you please copy me the full log of what you do ? After activating your env, you shoud do something like
    $ which pip
    $ cd $CPLEX_STUDIO_DIR1210/cplex/python/3.6/x86_64_linux/
    $ pip install .

I will need the full exact output of these commands to be able to help you.

bhimabi commented 4 years ago

Thanks alot. The pip install . seems to have worked. I shall run a few more files and confirm if it indeed works. I outlined what i did below. If you could figure out why what i did was wrong, that would be helpful to avoid such issues in future and perhaps for others too.

The installation of CPLEX 12.10 is at the following location /opt/ibm/ILOG/CPLEX_Studio1210.

  1. When i tired to install setup.py i ran the following commands while inside my virtual environment (created using virtualenv)
    $ cd  /opt/ibm/ILOG/CPLEX_Studio1210/cplex/python/3.6/x86_64_linux/
    $ sudo python3 setup.py install --home /home/asubbara/venv/lib/python3.6/site-packages/cplex     

That gives a long full output as below which did not seem to have any red flags. Is there a reason why this didnt work and the pip install . worked?

running install running build running build_py creating build creating build/lib creating build/lib/cplex copying cplex/aborter.py -> build/lib/cplex copying cplex/callbacks.py -> build/lib/cplex copying cplex/constant_class.py -> build/lib/cplex copying cplex/model_info.py -> build/lib/cplex copying cplex/paramset.py -> build/lib/cplex copying cplex/init.py -> build/lib/cplex creating build/lib/cplex/_internal copying cplex/_internal/_anno.py -> build/lib/cplex/_internal copying cplex/_internal/_aux_functions.py -> build/lib/cplex/_internal copying cplex/_internal/_baseinterface.py -> build/lib/cplex/_internal copying cplex/_internal/_callbackinfoenum.py -> build/lib/cplex/_internal copying cplex/_internal/_constants.py -> build/lib/cplex/_internal copying cplex/_internal/_constantsenum.py -> build/lib/cplex/_internal copying cplex/_internal/_list_array_utils.py -> build/lib/cplex/_internal copying cplex/_internal/_matrices.py -> build/lib/cplex/_internal copying cplex/_internal/_multiobj.py -> build/lib/cplex/_internal copying cplex/_internal/_multiobjsoln.py -> build/lib/cplex/_internal copying cplex/_internal/_ostream.py -> build/lib/cplex/_internal copying cplex/_internal/_parameters_auto.py -> build/lib/cplex/_internal copying cplex/_internal/_parameter_classes.py -> build/lib/cplex/_internal copying cplex/_internal/_parameter_hierarchy.py -> build/lib/cplex/_internal copying cplex/_internal/_procedural.py -> build/lib/cplex/_internal copying cplex/_internal/_pwl.py -> build/lib/cplex/_internal copying cplex/_internal/_pycplex.py -> build/lib/cplex/_internal copying cplex/_internal/_pycplex_platform.py -> build/lib/cplex/_internal copying cplex/_internal/_solutionstrategyenum.py -> build/lib/cplex/_internal copying cplex/_internal/_subinterfaces.py -> build/lib/cplex/_internal copying cplex/_internal/init.py -> build/lib/cplex/_internal creating build/lib/cplex/exceptions copying cplex/exceptions/errors.py -> build/lib/cplex/exceptions copying cplex/exceptions/error_codes.py -> build/lib/cplex/exceptions copying cplex/exceptions/init.py -> build/lib/cplex/exceptions copying cplex/_internal/py36_cplex12100.so -> build/lib/cplex/_internal running install_lib creating /home/asubbara/venv/lib/python3.6/site-packages/cplex creating /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib creating /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python creating /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex copying build/lib/cplex/aborter.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex copying build/lib/cplex/callbacks.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex copying build/lib/cplex/constant_class.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex copying build/lib/cplex/model_info.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex copying build/lib/cplex/paramset.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex copying build/lib/cplex/init.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex creating /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_anno.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_aux_functions.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_baseinterface.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_callbackinfoenum.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_constants.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_constantsenum.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_list_array_utils.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_matrices.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_multiobj.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_multiobjsoln.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_ostream.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_parameters_auto.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_parameter_classes.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_parameter_hierarchy.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_procedural.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_pwl.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_pycplex.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_pycplex_platform.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_solutionstrategyenum.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/_subinterfaces.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/init.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal copying build/lib/cplex/_internal/py36_cplex12100.so -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal creating /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions copying build/lib/cplex/exceptions/errors.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions copying build/lib/cplex/exceptions/error_codes.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions copying build/lib/cplex/exceptions/init.py -> /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/aborter.py to aborter.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/callbacks.py to callbacks.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/constant_class.py to constant_class.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/model_info.py to model_info.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/paramset.py to paramset.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/init.py to init.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_anno.py to _anno.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_aux_functions.py to _aux_functions.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_baseinterface.py to _baseinterface.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_callbackinfoenum.py to _callbackinfoenum.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_constants.py to _constants.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_constantsenum.py to _constantsenum.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_list_array_utils.py to _list_array_utils.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_matrices.py to _matrices.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_multiobj.py to _multiobj.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_multiobjsoln.py to _multiobjsoln.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_ostream.py to _ostream.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_parameters_auto.py to _parameters_auto.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_parameter_classes.py to _parameter_classes.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_parameter_hierarchy.py to _parameter_hierarchy.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_procedural.py to _procedural.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_pwl.py to _pwl.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_pycplex.py to _pycplex.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_pycplex_platform.py to _pycplex_platform.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_solutionstrategyenum.py to _solutionstrategyenum.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/_subinterfaces.py to _subinterfaces.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/_internal/init.py to init.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions/errors.py to errors.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions/error_codes.py to error_codes.cpython-36.pyc byte-compiling /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex/exceptions/init.py to init.cpython-36.pyc running install_egg_info Writing /home/asubbara/venv/lib/python3.6/site-packages/cplex/lib/python/cplex-12.10.0.0-py3.6.egg-info (venv) [asubbara@gounaris-r740-1 x86-64_linux]$ sudo python3 setup.py install --home /home/asubbara/venv/lib/python3.6/site-packages/cplex

vlkong commented 4 years ago

When you run python3, chances are this is the system installed python3, and as such will use the "base" packages.

pip install . will install in the base package unless you activated a virtual env. What you did was installing cplex in the virtual env, but you did not activate that virtual env. You should activate the virtual env first with source your/virtual/env/bin/activate.

Note also that installing a package in a virtual env using --home might work if you know exactly what you do, most of the time, you just risk installing the package at the wrong place. The regular way to do it is to activate the environment.

Please have a look at: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#activating-a-virtual-environment

bhimabi commented 4 years ago

That makes sense.

As a feedback : Perhaps editting the instructions on the cplex website might help others avoid this issue (unless I am the odd one out making this type of errors)

Thanks a lot for your help