ImportError: No module named 'cplex' Pytho

2019-07-27 02:43发布

问题:

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!

回答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).



回答2:

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.