How to properly install qpsolvers for Python on Windows?

Question:

I am trying to install qpsolvers using pip.
The installation goes without errors, and the module imports properly afterwards.

However, qpsolvers has no available solvers for it to use :

import qpsolvers

print(qpsolvers.available_solvers)

returns [].

Of course, trying to do anything results in an error:

SolverNotFound: solver 'quadprog' is not available

even on the exemple file for quadprog for instance.

I have checked the location of the package, and it looks like the solvers are there :
solver files in the package’s solvers folder

Uninstalling and reinstalling or trying older versions didn’t work.

How can I fix this?

Asked By: user18496484

||

Answers:

I install qpsolvers using pip install qpsolvers.
the link is here: https://pypi.org/project/qpsolvers/

I run the test code to check it works:

from numpy import array, dot
from qpsolvers import solve_qp
import qpsolvers

M = array([[1., 2., 0.], [-8., 3., 2.], [0., 1., 1.]])
P = dot(M.T, M)  # this is a positive definite matrix
q = dot(array([3., 2., 3.]), M).reshape((3,))
G = array([[1., 2., 1.], [2., 0., 1.], [-1., 2., -1.]])
h = array([3., 2., -2.]).reshape((3,))
A = array([1., 1., 1.])
b = array([1.])

x = solve_qp(P, q, G, h, A, b)
print("QP solution: x = {}".format(x))

it produces a result:

QP solution: x = [ 0.30769231 -0.69230769  1.38461538]

I also run the line of code in the question:

print(qpsolvers.available_solvers)

this also produces a result:

['quadprog']

So it all seems fine.
I am using vscode and windows10 with python3.9.

Answered By: D.L

I met this problem, and finally solved by installing the solver: pip install quadprog, which means that you need to install the solver independently. My python is 3.9 in Windows 10.

Answered By: 樊佳亮

Short answer: pip install qpsolvers[open_source_solvers]

This was indeed a bit confusing: qpsolvers is only an interface, by default it doesn’t decide which solvers to install for you (the files from your screenshot in the sub-directory are only interface files, not the solvers themselves).

With newer versions, when the module sees no QP solver at import time, it raises an exception with an advice to install open-source solvers:

>>> import qpsolvers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  ...
ImportError: no QP solver found, you can install some by running:

    pip install qpsolvers[open_source_solvers]

Hoping this helps!

Answered By: Tastalian
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.