diff --git a/doc/changelog.d/1060.added.md b/doc/changelog.d/1060.added.md new file mode 100644 index 000000000..f22dbd231 --- /dev/null +++ b/doc/changelog.d/1060.added.md @@ -0,0 +1 @@ +Create option for PyPIM to be installed separately \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 45e6fa9a3..8de54fd6a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,6 @@ dependencies = [ "ansys-api-mechanical==0.1.2", "ansys-mechanical-env==0.1.9", "ansys-mechanical-stubs==0.1.6", - "ansys-platform-instancemanagement>=1.0.1", "ansys-pythonnet>=3.1.0rc2", "ansys-tools-path>=0.3.1", "appdirs>=1.4.0", @@ -87,6 +86,9 @@ viz = [ "ansys-tools-visualization-interface>=0.2.6", "usd-core==24.11", ] +pim = [ + "ansys-platform-instancemanagement>=1.0.1", +] [project.scripts] ansys-mechanical = "ansys.mechanical.core.run:cli" diff --git a/src/ansys/mechanical/core/mechanical.py b/src/ansys/mechanical/core/mechanical.py index 6651f5ef8..7e472ef5a 100644 --- a/src/ansys/mechanical/core/mechanical.py +++ b/src/ansys/mechanical/core/mechanical.py @@ -32,12 +32,12 @@ import socket import threading import time +from typing import Optional +import warnings import weakref import ansys.api.mechanical.v0.mechanical_pb2 as mechanical_pb2 import ansys.api.mechanical.v0.mechanical_pb2_grpc as mechanical_pb2_grpc -import ansys.platform.instancemanagement as pypim -from ansys.platform.instancemanagement import Instance import ansys.tools.path as atp import grpc @@ -57,6 +57,16 @@ threaded, ) +# Check if PyPIM is installed +try: + import ansys.platform.instancemanagement as pypim # pragma: nocover noqa: F401 + + _HAS_ANSYS_PIM = True + """Whether or not PyPIM exists.""" +except ImportError: + _HAS_ANSYS_PIM = False + + # Checking if tqdm is installed. # If it is, the default value for progress_bar is true. try: @@ -1932,7 +1942,9 @@ def launch_grpc( return port -def launch_remote_mechanical(version=None) -> (grpc.Channel, Instance): # pragma: no cover +def launch_remote_mechanical( + version=None, +) -> (grpc.Channel, Optional["Instance"]): # pragma: no cover """Start Mechanical remotely using the Product Instance Management (PIM) API. When calling this method, you must ensure that you are in an environment @@ -1951,6 +1963,13 @@ def launch_remote_mechanical(version=None) -> (grpc.Channel, Instance): # pragm ------- Tuple containing channel, remote_instance. """ + # Display warning if PyPIM is not installed + if not _HAS_ANSYS_PIM: + warnings.warn( + "Installation of pim option required! Use ``pip install ansys-mechanical-core[pim]``." + ) + return + pim = pypim.connect() instance = pim.create_instance(product_name="mechanical", product_version=version) @@ -2062,7 +2081,7 @@ def launch_mechanical( version : str, optional Mechanical version to run in the three-digit format. For example, ``"251"`` for 2025 R1. The default is ``None``, in which case the server runs the - latest installed version. If PyPIM is configured and ``exce_file=None``, + latest installed version. If PyPIM is configured and ``exec_file=None``, PyPIM launches Mechanical using its ``version`` parameter. keep_connection_alive : bool, optional Whether to keep the gRPC connection alive by running a background thread @@ -2099,7 +2118,7 @@ def launch_mechanical( """ # Start Mechanical with PyPIM if the environment is configured for it # and a directive on how to launch Mechanical was not passed. - if pypim.is_configured() and exec_file is None: # pragma: no cover + if _HAS_ANSYS_PIM and pypim.is_configured() and exec_file is None: # pragma: no cover LOG.info("Starting Mechanical remotely. The startup configuration will be ignored.") channel, remote_instance = launch_remote_mechanical(version=version) return Mechanical( diff --git a/src/ansys/mechanical/core/pool.py b/src/ansys/mechanical/core/pool.py index fe0e09930..e1da03a70 100644 --- a/src/ansys/mechanical/core/pool.py +++ b/src/ansys/mechanical/core/pool.py @@ -26,11 +26,11 @@ import time import warnings -import ansys.platform.instancemanagement as pypim from ansys.tools.path import version_from_path from ansys.mechanical.core.errors import VersionError from ansys.mechanical.core.mechanical import ( + _HAS_ANSYS_PIM, _HAS_TQDM, LOG, MECHANICAL_DEFAULT_PORT, @@ -175,7 +175,7 @@ def __init__( if "exec_file" in kwargs: exec_file = kwargs["exec_file"] else: - if pypim.is_configured(): # pragma: no cover + if _HAS_ANSYS_PIM and pypim.is_configured(): # pragma: no cover if "version" in kwargs: version = kwargs["version"] self._remote = True