Mosek license file can no longer be found after #2326
Describe the bug
Setting MOSEKLM_LICENSE_FILE in the current processes environment prior to using cvxpy no longer works.
To Reproduce
import os
from cvxpy.tests.solver_test_helpers import StandardTestLPs
os.environ['MOSEKLM_LICENSE_FILE'] = '/path/to/mosek.lic'
StandardTestLPs.test_lp_0(solver='MOSEK')
Expected behavior I expect the code above to continue working.
Output This now raises:
mosek.Error: rescode.err_missing_license_file(1008): License cannot be located. The default search path is ':/home/user/mosek/mosek.lic:'
See: #2326
For anyone looking for a workaround, the following works:
import mosek
from cvxpy.tests.solver_test_helpers import StandardTestLPs
mosek.Env(
globalenv=True,
licensefile='/path/to/mosek.lic',
)
StandardTestLPs.test_lp_0(solver='MOSEK')
@apmorton which mosek and cvxpy version are you using?
mosek 10.1.31 cvxpy 1.5.1
In Mosek 10 with the global environment the path to the license file must be known before the first time Mosek is loaded. So an easier hack would be to first set the variable in os.environ and then "import mosek", or in any case set the environment before the first import of anything from mosek, cvxpy etc. that could load Mosek. I don't guarantee it works on Windows.
The only true and correct solution of course is to never change the environment inside the process but set everything before calling the process.
Thanks @aszekMosek for the explanation, and glad that @apmorton found a workaround. Does not appear to be directly related to a change in CVXPY, so I'm going ahead and close this issue for now.
I'm not sure that is a fair assessment - this is the direct result of a change in cvxpy - #2326
Prior to then cvxpy wouldn't initialize mosek until the first usage, now it initializes it at import time.
This is a noticeable behavior change and broke our usage of cvxpy.
@aszekMosek is the workaround I posted above something supported by mosek?
@apmorton sorry about that, misread the issue to be a change in mosek behavior!
Both before and after #2326 CVXPY did not support any official mechanism to pass a license path to Mosek, so nothing changed in this respect. I would argue that both your solutions before and after #2326 are to some extent semi-supported hacks which one way or another try to bypass the one official mechanism applicable in this situation, which is to have MOSEKLM_LICENSE_FILE set in the initial environment of the process.
Yes, the workaround works because it is ultimately equivalent to calling the API method env.putlicensepath() on the global Mosek environment created when Mosek is first loaded. As of now it is not officially supported.
If you would like to then please contact Mosek support and we can think what else can be done about that.
We have been relying on the ability to set MOSEKLM_LICENSE_FILE path at runtime in production for many years.
The requirement to know this path and set an environment variable prior to process creation in all possible scenarios where mosek will be used is very tedious.
We will be reaching out to support on this matter since having the ability to set this at runtime is important to our business.
We have some idea for what we could change so that you get an official, supported solution, but that will be in Mosek version 10.2, ie. the current one. We can discuss it in support.
Just to summarize the other current options for how to modify the license path in the global Mosek environment:
- your solution with
mosek.Env(globalenv=True).putlicensepath("/path/...")(works, relies on unofficial behavior) - setting
os.environbefore the first time anymosekis loaded (Linux and OSX only, relies on unofficial behavior) - using Fusion as a proxy:
mosek.fusion.Model.putlicensepath("/path/...")(works, official for the Fusion API, unnatural in the context of cvxpy)
The proposed solution we have in mind goes towards cleaning up and legalizing something along the lines of 1. Until you can use that then the solution in 1. is probably best.
We also encountered this. Thanks for the workaround. It would be ideal if this was added to the official documentation. I almost missed this ticket.
@Hnasar For what it's worth I added an answer in the Mosek FAQ https://docs.mosek.com/latest/faq/faq.html#why-is-os-environ-moseklm-license-file-ignored-by-mosek