I have observed, for example in https://pyomo.readthedocs.io/en/stable/working_models.html, that sometimes SolverFactory is imported from pyomo.opt and sometimes from pyomo.environ. Is there any difference? Is a matter of legacy vs new syntax?
There is no difference: they are the same object. The examples that import it from pyomo.opt
are just older and date from before SolverFactory
was added to the pyomo.environ
namespace.
The intent behind pyomo.environ
was to gather the classes and objects most commonly used by users into a single namespace. This was more of an issue before Pyomo 4.0, when the project was called "Coopr" and was distributed as a series of pseudo-independent packages. That meant the main namespace (what is now "pyomo") was a "namespace package" and didn't have objects / classes in it.
pyomo.environ
also takes care of importing and registering the plugins that are scattered around the code base (in particular: solvers, writers, and transformations). In that sense, it is generally important to always import pyomo.environ
(or something from pyomo.environ.*
) as part of any model, otherwise it is likely that the solver you want to use was not registered with the SolverFactory
.