From e72769ac1ffdec907816e8f6091effbaf04d754b Mon Sep 17 00:00:00 2001 From: Ella Wu <602725+nwu63@users.noreply.github.com> Date: Sat, 16 Mar 2024 21:34:51 -0700 Subject: [PATCH] cleanup paropt import --- pyoptsparse/pyIPOPT/pyIPOPT.py | 1 - pyoptsparse/pyOpt_utils.py | 2 +- pyoptsparse/pyParOpt/ParOpt.py | 38 ++++++++++++---------------------- pyoptsparse/pySLSQP/pySLSQP.py | 1 - tests/test_snopt_bugfix.py | 1 - tests/test_user_termination.py | 1 - tests/testing_utils.py | 1 - 7 files changed, 14 insertions(+), 31 deletions(-) diff --git a/pyoptsparse/pyIPOPT/pyIPOPT.py b/pyoptsparse/pyIPOPT/pyIPOPT.py index e9ff43a8..2e831678 100644 --- a/pyoptsparse/pyIPOPT/pyIPOPT.py +++ b/pyoptsparse/pyIPOPT/pyIPOPT.py @@ -12,7 +12,6 @@ import numpy as np # Local modules -from ..pyOpt_error import Error from ..pyOpt_optimizer import Optimizer from ..pyOpt_utils import ( ICOL, diff --git a/pyoptsparse/pyOpt_utils.py b/pyoptsparse/pyOpt_utils.py index 5c5826d8..05dd87d1 100644 --- a/pyoptsparse/pyOpt_utils.py +++ b/pyoptsparse/pyOpt_utils.py @@ -592,9 +592,9 @@ def try_import_compiled_module_from_path(module_name: str, path: str|None = None If importable, the imported module is returned. If not importable, the error message is instead returned. """ - path = os.path.abspath(os.path.expandvars(os.path.expanduser(path))) orig_path = sys.path if path is not None: + path = os.path.abspath(os.path.expandvars(os.path.expanduser(path))) sys.path = [path] try: module = importlib.import_module(module_name) diff --git a/pyoptsparse/pyParOpt/ParOpt.py b/pyoptsparse/pyParOpt/ParOpt.py index 46fb92f7..4198454e 100644 --- a/pyoptsparse/pyParOpt/ParOpt.py +++ b/pyoptsparse/pyParOpt/ParOpt.py @@ -6,35 +6,22 @@ # External modules import numpy as np -# isort: off -# Attempt to import mpi4py. +# Local modules +from ..pyOpt_optimizer import Optimizer +from ..pyOpt_utils import INFINITY, try_import_compiled_module_from_path + +# Attempt to import ParOpt/mpi4py # If PYOPTSPARSE_REQUIRE_MPI is set to a recognized positive value, attempt import # and raise exception on failure. If set to anything else, no import is attempted. -if "PYOPTSPARSE_REQUIRE_MPI" in os.environ: - if os.environ["PYOPTSPARSE_REQUIRE_MPI"].lower() in ["always", "1", "true", "yes"]: - try: - from paropt import ParOpt as _ParOpt - from mpi4py import MPI - except ImportError: - _ParOpt = None - else: - _ParOpt = None +if "PYOPTSPARSE_REQUIRE_MPI" in os.environ and os.environ["PYOPTSPARSE_REQUIRE_MPI"].lower() not in ["always", "1", "true", "yes"]: + _ParOpt = "ParOpt was not imported, as requested by the environment variable 'PYOPTSPARSE_REQUIRE_MPI'" + MPI = "mpi4py was not imported, as requested by the environment variable 'PYOPTSPARSE_REQUIRE_MPI'" # If PYOPTSPARSE_REQUIRE_MPI is unset, attempt to import mpi4py. # Since ParOpt requires mpi4py, if either _ParOpt or mpi4py is unavailable # we disable the optimizer. else: - try: - from paropt import ParOpt as _ParOpt - from mpi4py import MPI - except ImportError: - _ParOpt = None -# isort: on - -# Local modules -from ..pyOpt_error import Error -from ..pyOpt_optimizer import Optimizer -from ..pyOpt_utils import INFINITY - + _ParOpt = try_import_compiled_module_from_path("paropt.ParOpt") + MPI = try_import_compiled_module_from_path("mpi4py.MPI") class ParOpt(Optimizer): """ @@ -48,8 +35,9 @@ class ParOpt(Optimizer): def __init__(self, raiseError=True, options={}): name = "ParOpt" category = "Local Optimizer" - if _ParOpt is None and raiseError: - raise ImportError("There was an error importing ParOpt") + for mod in [_ParOpt, MPI]: + if isinstance(mod, str) and raiseError: + raise ImportError(mod) # Create and fill-in the dictionary of default option values self.defOpts = {} diff --git a/pyoptsparse/pySLSQP/pySLSQP.py b/pyoptsparse/pySLSQP/pySLSQP.py index e5ec91fa..e5a5eac1 100644 --- a/pyoptsparse/pySLSQP/pySLSQP.py +++ b/pyoptsparse/pySLSQP/pySLSQP.py @@ -12,7 +12,6 @@ import numpy as np # Local modules -from ..pyOpt_error import Error from ..pyOpt_optimizer import Optimizer from ..pyOpt_utils import try_import_compiled_module_from_path diff --git a/tests/test_snopt_bugfix.py b/tests/test_snopt_bugfix.py index 40b00d12..d3c8b598 100644 --- a/tests/test_snopt_bugfix.py +++ b/tests/test_snopt_bugfix.py @@ -12,7 +12,6 @@ # First party modules from pyoptsparse import SNOPT, Optimization -from pyoptsparse.pyOpt_error import Error def objfunc(xdict): diff --git a/tests/test_user_termination.py b/tests/test_user_termination.py index bf47f007..a857a9f1 100644 --- a/tests/test_user_termination.py +++ b/tests/test_user_termination.py @@ -14,7 +14,6 @@ # First party modules from pyoptsparse import OPT, Optimization -from pyoptsparse.pyOpt_error import Error class TerminateComp: diff --git a/tests/testing_utils.py b/tests/testing_utils.py index 667a2942..fce54c26 100644 --- a/tests/testing_utils.py +++ b/tests/testing_utils.py @@ -9,7 +9,6 @@ # First party modules from pyoptsparse import OPT, History -from pyoptsparse.pyOpt_error import Error def assert_optProb_size(optProb, nObj, nDV, nCon):