Note
Go to the end to download the full example code.
Sherlock Project Import in Single Mode#
This example demonstrates how to launch the Sherlock gRPC service in single-project mode, import a project, and handle common exceptions during the import process.
Description Sherlock’s gRPC API enables automation of various workflows, including project management. This script demonstrates how to: - Connect to the Sherlock service in single-project mode. - Import a sample project archive. - Handle import errors gracefully.
import os
from examples.examples_globals import (
get_sherlock_tutorial_path,
get_temp_dir,
store_sherlock_tutorial_path,
)
from ansys.sherlock.core import launcher
from ansys.sherlock.core.errors import SherlockImportProjectZipArchiveSingleModeError
Launch Sherlock in single project mode#
Launch Sherlock on Windows in headless mode using Windows Named User Authentication for the gRPC transport mode, specifying the project path. See the API reference for how to launch Sherlock on Linux or Windows using other options.
sherlock, ansys_install_path = launcher.launch_and_connect(
port=9093,
single_project_path=os.getcwd(),
transport_mode="wnua",
sherlock_command_args="-noGUI",
)
store_sherlock_tutorial_path(ansys_install_path)
Traceback (most recent call last):
File "C:\Users\ansys\actions-runner\_work\pysherlock\pysherlock\examples\02-importing\import_project_zip_archive_single_mode.py", line 63, in <module>
sherlock, ansys_install_path = launcher.launch_and_connect(
~~~~~~~~~~~~~~~~~~~~~~~~~~~^
port=9093,
^^^^^^^^^^
...<2 lines>...
sherlock_command_args="-noGUI",
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "C:\Users\ansys\actions-runner\_work\pysherlock\pysherlock\.venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 246, in launch_and_connect
sherlock = connect(
port=port,
...<3 lines>...
uds_id=uds_id,
)
File "C:\Users\ansys\actions-runner\_work\pysherlock\pysherlock\.venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 324, in connect
raise e
File "C:\Users\ansys\actions-runner\_work\pysherlock\pysherlock\.venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 306, in connect
_wait_for_sherlock_grpc_ready(channel, timeout)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
File "C:\Users\ansys\actions-runner\_work\pysherlock\pysherlock\.venv\Lib\site-packages\ansys\sherlock\core\launcher.py", line 395, in _wait_for_sherlock_grpc_ready
raise SherlockConnectionError(message="Error starting gRPC service")
ansys.sherlock.core.errors.SherlockConnectionError: gRPC connection error: Error starting gRPC service
Import Sherlock Project in single project mode#
Import a tutorial project ZIP archive provided with the Sherlock installation.
try:
sherlock.project.import_project_zip_archive_single_mode(
project="Test",
category="Demos",
archive_file=os.path.join(get_sherlock_tutorial_path(), "Auto Relay Project.zip"),
destination_file_directory=get_temp_dir(),
)
print("Tutorial project imported successfully.")
except SherlockImportProjectZipArchiveSingleModeError as e:
print("Error importing project.")
Exit Sherlock#
Exit the gRPC connection and shut down Sherlock.
sherlock.common.exit(True)
Total running time of the script: (2 minutes 0.045 seconds)