coin-or / python-mip

Python-MIP: collection of Python tools for the modeling and solution of Mixed-Integer Linear programs
Eclipse Public License 2.0
525 stars 92 forks source link

Catch ImportError when trying to import numpy #105

Closed jurasofish closed 4 years ago

jurasofish commented 4 years ago

In 5b9aa03f0b4f65822d578d7c7ba0f237350059f2 importing numpy excepts the base Exception class to indicate that numpy isn't available. This is too broad an exception and will catch exceptions that it shouldn't. Catching ImportError is what the code really intends to do.

tuliotoffolo commented 4 years ago

Hello Michael, We raise a ModuleNotFoundError error when someone tries to use numpy-related features and numpy is not available, not the base Exception. The idea is to make numpy optional. Am I missing something?

jurasofish commented 4 years ago

I made PR #106 to show what I mean - it's generally desirable to catch the most specific exception possible. Especially in the case of the ndarray.py file, where any issue creating that class would have been caught and treated as though numpy wasn't available. By catching only ImportError we ensure that only import issues are caught. The logging means that if the user expects numpy to import properly but there's an issue importing numpy the user can look at the debug level logs to help them figure out why it's not importing.

tuliotoffolo commented 4 years ago

Thanks!