Simcenter Amesim How to use Amesim API modules in Spyder IDE

2024-06-26T10:57:39.000-0400
Simcenter Amesim Automation Connect Simcenter System Simulation Client for Git Simcenter Amesim Teamcenter Share

Summary

This article explains how to use the Amesim API modules in Spyder IDE. It covers setting up a Python 3.8 environment with Anaconda, tweaking Spyder settings to match a specific Amesim version, and some steps to ensure Amesim APIs work with this setup. Follow along to get Amesim and Spyder working together without any hitches.


Details

Introduction

Spyder is a popular IDE tailored for Python. It offers a user-friendly workspace with a rich set of tools, like an interactive console, advanced debugging capabilities, and an intuitive editor. Integrating Simcenter Amesim with Spyder can streamline beginners and advanced user's workflow. Instead of switching between platforms, they can use the power of Amesim modules directly within the Spyder environment, making their simulation and modeling automatization tasks smoother and more efficient.


Step 1: Create a dedicated environment based on a Python 3.8 version (minor index doesn’t matter)

By creating a specific environment for Amesim, you isolate the setup and ensure that any packages you install don't interfere with other projects. APIs module's dependency on Python 3.8 highlights the importance of version-specific environments.



This is important, because the Amesim API modules are extensions that require to be loaded by a Python 3.8 version. This environment will allow us to install our needed modules, in order to avoid to install them directly in the Amesim installation folder.


Step 2: Install the spyder-kernels package required by Spyder to support the console.

Spyder relies on the spyder-kernels package. This package allows Spyder's console to execute Python code, display outputs, and offer debugging capabilities. Without it, the interactive nature of Spyder is compromised.



Additional modules that your scripts require need to be added from here as well (or using the prompt of the new Amesim environment).


Step 3: Select the python interpreter associated with the new environment created in Anaconda.

When using Spyder, it can tap into any Python environment you have on your machine. By specifically pointing Spyder to the environment you set up for Amesim, you ensure that any Amesim-related code you run will have access to the necessary libraries and the correct Python version. This setting ensures consistency and reduces the chance of runtime errors.

Install Spyder in the new Amesim environment and adjust its preferences settings to use the interpreter associated to it.



(Type conda info in the new Amesim environment prompt if you can’t find the location directly)
 


Step 4: Configure Python to Access and Load Amesim's API Modules and Dependencies

Now we need to make Python be able to find and load the Amesim API modules and dlls. In order to locate them we add them to the PYTHONPATH environment variable.

image.png

However, this is not enough. The APIs depend on Amesim binaries DLLs that need to be found. Normally, these are found in the PATH environment variable, but starting from the Python 3.8, those DLLs search paths need to be explicitly added using the add_dll_directory from the os module.

import os
# Add Amesim DLLs directory
os.add_dll_directory(os.path.join(os.getenv("AME"), "win64"))
import ame_apy

This step is done automatically when using the Python interpreter from the Amesim installation. However, when using a different interpreter, we must manually add these paths. You can use the following line to populate the amesim_hook.pth file (use the correct Amesim installation path

import os
os.environ["CONDA_DLL_SEARCH_MODIFICATION_ENABLE"] = "1"
os.add_dll_directory(r"C:\Program Files\Simcenter\2210\Amesim\win64")
os.add_dll_directory(r"C:\Program Files\Simcenter\2210\Amesim\scripting\python")
os.add_dll_directory(r"C:\Program Files\Simcenter\2210\Amesim\scripting\python\win64")


The trick consists in adding a .pth file with above lines in the Anaconda environment site_packages directory that will be loaded and executing at start time, in order to:

  • Add Amesim python module search paths

  • Add Amesim binaries search paths



Additional step:

An additional step (for certain API modules) is to ensure above script runs whenever you start a Python session. There are several alternatives for this:

  • Spyder Startup Script: Configure Spyder to automatically run it at startup. This is done in Spyder’s settings under Tools > Preferences > IPython console > Startup.
image.png
  • Python Startup Variable: Use a Python startup file (PYTHONSTARTUP environment variable) to run the script auomatically in each Python interactive session.
image.png
  • ​​Manual Calls: Add manually at the beginning of each session or in each script where you need access to the Amesim API modules.
image.png
they only need to be called once, and then can be deleted from your script.

Normally, this should allow us to use Amesim modules in Spyder IDE.

 
 
 
 

KB Article ID# KB000122254_EN_US

Contents

SummaryDetails

Associated Components

Amesim Automation Connect