2

I am trying to run python scripts on a HPC machine (RedHatEntrepriseServer). CPLEX module is already installed by adminastrators but I still have this error in importing cplex module in python.

ImportError: No module named 'cplex'

I know it is common error and I found that I may need to update PYTHONPATH variable with cplex home directory, but I can't find this home directory. Does anyone have an idea or suggestion to solve this problem?!

Many thanks!

Betty
  • 237
  • 6
  • 16

2 Answers2

1

As mentioned in the comments, setting the PYTHONPATH environment variable is a perfectly reasonable solution here. Using PYTHONPATH has advantages, such as allowing you to easily switch between multiple versions of the CPLEX Python API. However, another option would be to ask your system administrator to install the cplex module. My guess is that they installed CPLEX Optimization Studio, but they did not actually install the CPLEX Python API into the system's default Python interpreter using the setup.py script (that is an optional step that would need to be performed separately). See the relevant documentation here. Yet another option would be for you to setup your own Python environment using virtualenv (see, for example, here).

rkersh
  • 4,447
  • 2
  • 22
  • 31
  • the administrators have already installed cplex module but the access to cplex directory was hidden. Thank you for the additional information! – Betty Jul 22 '17 at 22:47
  • 1
    Based on the path you're using in your `PYTHONPATH` the cplex module has *not* been installed using the `setup.py` script. Under normal circumstances, we would expect to see a [site-packages](https://stackoverflow.com/questions/31384639/what-is-pythons-site-packages-directory) directory in the path. The COS installer copies the files on to the hard-drive, but it does not put them in the Python search path. If `setup.py` had been used, then you would not need to set `PYTHONPATH`. At any rate, I'm glad you found a solution. – rkersh Jul 24 '17 at 19:58
0

Thanks to skr and danche comments, here is how I solved the problem:

I looked for cplex location by using this command:

find / -iname "cplex"

Then I updated the PYTHONPATH variable with this location:

export PYTHONPATH=$PYTHONPATH:/local/software/cplex/12.6.1/cplex/python/2.6/x86-64_linux

I updated my .bashrc file with last command so each time I log in it is set automatically.

Betty
  • 237
  • 6
  • 16