How to setup IronPython environment in VS and run an external Python script
Giulia M.
Member, Employee Posts: 19
✭✭✭
How to set up Python environment in VS 2019 in order to run external Python script?
Tagged:
0
Answers
-
IronPython environment setup in VS: Tools > Python > Python Environments > Add Environments
A new window should pop up, here add a new custom environment and point the following fields to:
- Prefix path: C:\Program Files\AnsysEM\AnsysEM20.1\Win64\common\IronPython
- Description: ANSYS IronPython
- Interpreter path: C:\Program Files\AnsysEM\AnsysEM20.1\Win64\common\IronPython\ipy64.exe
- Windowed interpreter (optional): C:\Program Files\AnsysEM\AnsysEM20.1\Win64\common\IronPython\ipyw64.exe
Bear in mind to change the path according to the Ansys version in use.
Now if we want to run an external script in the specified Python environment, open the interactive window on the right hand side.
When creating a script outside of Electronics Desktop, the following lines should be included at the beginning and at the last line of your script:
import clr import os import sys from Microsoft.Win32 import Registry global AnsysStandalone, AnsysInstallDirectory, Desktop latestInstalledVersion = Registry.GetValue("HKEY_CLASSES_ROOT\Ansoft.ElectronicsDesktop\CurVer", "", "") desktopVersion = latestInstalledVersion[-6:] installDirectory = Registry.GetValue("HKEY_LOCAL_MACHINE\Software\Ansoft\ElectronicsDesktop\{}\Desktop".format(desktopVersion), "InstallationDirectory", '') sys.path.append(installDirectory) clr.AddReference("Ansys.Ansoft.DesktopPlugin") clr.AddReference("Ansys.Ansoft.CoreCOMScripting") clr.AddReference("Ansys.Ansoft.PluginCoreDotNet") sys.path.append(os.path.join(installDirectory, 'PythonFiles','DesktopPlugin')) import ScriptEnv ScriptEnv.Initialize('Ansoft.ElectronicsDesktop.{}'.format(desktopVersion)) Desktop = sys.modules['__main__'].oDesktop Desktop.RestoreWindow() oProject = oDesktop.NewProject() //Insert the wanted design oProject.InsertDesign("Design", "DesignName", "SolutionType", "") .. //Do what you want here with your design .. ScriptEnv.Shutdown()
0