Skip to content

Commit b986739

Browse files
committed
Add support for gurobi installed via gurobipy from pip (#185)
1 parent 1446673 commit b986739

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878

7979
- name: Install dependencies CPython
8080
if: ${{ matrix.python-version != 'pypy3' }}
81-
run: python -m pip install cffi pytest networkx numpy matplotlib
81+
run: python -m pip install cffi pytest networkx numpy matplotlib gurobipy
8282

8383
- name: Install dependencies PyPy
8484
if: ${{ matrix.python-version == 'pypy3' && matrix.os != 'macos-latest' }}
@@ -94,7 +94,14 @@ jobs:
9494
- name: list installed packages
9595
run: python -m pip list
9696

97-
- name: run tests
97+
- name: Run tests PyPy
98+
if: ${{ matrix.python-version == 'pypy3'}}
99+
run: |
100+
python -m pytest test --verbose --color=yes --doctest-modules --ignore="test/test_gurobi.py"
101+
python -m pytest mip --verbose --color=yes --doctest-modules --ignore="mip/gurobi.py"
102+
103+
- name: Run tests
104+
if: ${{ matrix.python-version != 'pypy3'}}
98105
run: |
99106
python -m pytest test --verbose --color=yes --doctest-modules
100107
python -m pytest mip --verbose --color=yes --doctest-modules --ignore="mip/gurobi.py"

mip/gurobi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from os import environ
99
import numbers
1010
from cffi import FFI
11+
import importlib.util
12+
import pathlib
1113
from mip.exceptions import (
1214
ParameterNotAvailable,
1315
SolutionNotAvailable,
@@ -84,6 +86,18 @@
8486
if lib_path is not None:
8587
break
8688

89+
# Check for gurobi 9.5 through pip installation
90+
if lib_path is None:
91+
gurobipy_spec = importlib.util.find_spec("gurobipy")
92+
if gurobipy_spec:
93+
parent_path = pathlib.Path(gurobipy_spec.origin).parent
94+
if platform.lower().startswith("win"):
95+
extension = ".dll"
96+
else:
97+
extension = ".so"
98+
lib_path = str(next(parent_path.glob(f"*{extension}")))
99+
100+
87101
if lib_path is None:
88102
found = False
89103
else:

test/test_gurobi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
import mip
4+
5+
6+
def test_gurobi_pip_installation():
7+
# Even though we have no valid license yet, we could check that the binaries are found.
8+
# If no valid license is found, an InterfacingError is thrown
9+
10+
with pytest.raises(mip.InterfacingError):
11+
mip.Model(solver_name="GRB")
12+

0 commit comments

Comments
 (0)