0

I am installing Cplex python API into my MacBook. So I followed the tutorial given in YouTube (https://www.youtube.com/watch?v=oINxanpv_2M) and have installed the script & library for Cplex using the Terminal instructions:

cd /Users/my_name/IBM_CPLEX/CPLEX_Studio1210/python
python3 setup.py install

this part works alright with everything installed, just a note from the Terminal at the beginning saying 'python wrapper is not found'.

Then I tested an existing Cplex problem using

cd IBM_CPLEX/CPLEX_Studio1210/python/examples/mp/modeling
python3 production.py

, which returned:

Model: production
 - number of variables: 6
   - binary=0, integer=0, continuous=6
 - number of constraints: 5
   - linear=5
 - parameters: defaults
Traceback (most recent call last):
  File "production.py", line 101, in <module>
    if model.solve():
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/docplex/mp/model.py", line 4152, in solve
    return self.fatal("CPLEX runtime not found: please install CPLEX or solve this model on DOcplexcloud")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/docplex/mp/model.py", line 884, in fatal
    self._error_handler.fatal(msg, args)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/docplex/mp/error_handler.py", line 210, in fatal
    raise DOcplexException(resolved_message)
docplex.mp.utils.DOcplexException: CPLEX runtime not found: please install CPLEX or solve this model on DOcplexcloud

So my initial guess is that my python 3 has been installed in another directory under the Library folder, and a path will be needed to link the Cplex engine to the python solver (if correct). Inside the YouTube tutorial, a PATH is actually provided, so in my case it's written as:

PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
export PYTHONPATH=/Users/my_name/IBM_CPLEX/CPLEX_Studio1210/cplex/python/3.7/x86-64_osx

I have actually installed Cplex python API into a Windows PC previously. The path-adding step was done by visiting: Control Panel -> System and Security -> System -> Advanced system settings -> Environment Variables. Visit (https://www.youtube.com/watch?v=hqGZzRh00y0) for specific further steps.

However, I tried quite a lot of ways adding this path to my Mac, and so far I've used those to write the above path but none worked:

nano .bash_profile.pysave
nano .bash_profile
sudo nano /etc/paths

That's where I got stuck. Any help and comment would be highly appreciated. I am new to Mac Terminal. Anaconda is also installed in my Mac if that's helpful.

Sorry for the massive details - was trying to put down everything in case there was something unnoticed that I have done wrong. Thanks.


Added: A small issue in the process above was that my Python was actually 3.8. So after adding the path export PYTHONPATH=/Users/my_name/IBM_CPLEX/CPLEX_Studio1210/cplex/python/3.7/x86-64_osx and running a cplex example, a message popped up inside the Terminal:

Exception: CPLEX 12.10.0.0 is not compatible with this version of Python.

Need to download an older 3.7 version then did the $ python3.7 setup.py install process again after changing my directory to IBM_CPLEX/CPLEX_Studio1210/python.

Some similar issues about Python version have been discussed in the post: CPLEX and Python 3.7.

LuciaEv
  • 13
  • 4
  • The first thing to try is to run `echo $PYTHONPATH` in the console to make sure the environment variable is indeed set to the value it should be. Can you check that? – Daniel Junglas Aug 05 '20 at 05:04
  • Hi Daniel, I restarted the Terminal and echo $PYTHONPATH returns a blank line. Right, it seems the variable is not set. – LuciaEv Aug 05 '20 at 07:50
  • Where exactly did you set this variable? If you type `export PYTHONPATH=/Users/my_name/IBM_CPLEX/CPLEX_Studio1210/cplex/python/3.7/x86-64_osx` inside the terminal and then run your program then this should work. – Daniel Junglas Aug 05 '20 at 08:30
  • Yes thanks the line works well (also need to download python3.7 to replace 3.8 for compatibility), now I'm able to solve any ```.py``` file using the Terminal. Part of the output shows: \\ ```* system is: Darwin 64bit * Python version 3.7.6, located at: /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 * docplex is present, version is (2, 11, 176) * CPLEX library is present, version is 12.10.0.0, located at: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages ``` – LuciaEv Aug 05 '20 at 10:56
  • But I’m not able to use Anaconda Jupyter to solve any of the ```.ipynb``` files, or IDLE for ```.py``` files. Still have the ```ModuleNotFoundError: No module named 'docplex' ``` I’m thinking if it’s because another path might be needed to lead Anaconda to Cplex as well? – LuciaEv Aug 05 '20 at 10:58
  • This indicates that `docplex` is not in your `PYTHONPATH` or was installed in a bad location. I suggest that in your notebook you do `import os` and `print(os.environ['PYTHONPATH'])`. This way you can see the value of the variable the same way Jupyter sees it. Compare this to what you get for `echo $PYTHONPATH` on the console. If it is not the same then you know that Jupyter uses the wrong value. You may either set PYTHONPATH globally for your system or maybe in the Jupyter configuration. – Daniel Junglas Aug 05 '20 at 13:06

0 Answers0