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
518 stars 92 forks source link

Fix import errors due to conditional absolute imports #155

Closed lpsinger closed 2 years ago

lpsinger commented 3 years ago

There were import errors due to redefinition of the top-level mip module through conditional absolute imports when explicitly specifying the solver:

>>> import mip
>>> mip.Model()
<mip.model.Model object at 0x10f576700>
>>> mip.Model(solver=mip.CBC)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../mip/model.py", line 104, in __init__
    self.constrs = mip.ConstrList(self)
UnboundLocalError: local variable 'mip' referenced before assignment
>>> mip.Model(solver=mip.GUROBI)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../mip/model.py", line 104, in __init__
    self.constrs = mip.ConstrList(self)
UnboundLocalError: local variable 'mip' referenced before assignment

This patch fixes these imprt errors through selective use of relative imports.

sebheger commented 2 years ago

@lpsinger

Thank you for opening this PR. It's been a long time open, I now did some analysis. There might be an import issue where you suggest a fix, but you feed the keyword solver with the solver_name (by the values of the "name" enum). So your reported errors by the calls occur because nothing from mip is imported. All your adaptions in the code are not reached by the calls. With solver keyword a mipSolver instance should be passed. I am still trying to understand why and how to pass a mipSolver instance, as the mipSolver self contains the model?

lpsinger commented 2 years ago

Sorry, I don't remember. I'm not using python-mip any more.

sebheger commented 2 years ago

Thanks for the information/update @lpsinger

Thus, I will close this PR.