Ansys Assistant will be unavailable on the Learning Forum starting January 30. An upgraded version is coming soon. We apologize for any inconvenience and appreciate your patience. Stay tuned for updates.
General Mechanical

General Mechanical

Topics related to Mechanical Enterprise, Motion, Additive Print and more.

Clearing the Python shell namespace in Mechanical Scripting

    • mhorn2
      Subscriber

      Hello,

      I am developing a python script to automatically generate some named selections. I am using 2021R1 Mechanical Scripting.

      I am wondering how to clear all the variables that I have defined from the namespace, or better yet, restart the "Shell".

      When I develop python code outside of ANSYS I often restart the iPython console to provide a fresh namespace. This prevents a script from successfully running, when there are indeed errors. The error I am concerned about is when a script accesses a variable before its definition. If I am developing a code in a shell that has stale variables from a previous run, then they could be accessed without issue by my script that has this error. When I run the script in a new Mechanical session it will error out.

      PS - scripting is an AWESOME feature. The 2021R1 updates are making a world of difference for me. Good stuff.

      Any help is appreciated. Thanks!

      Mike

    • mhorn2
      Subscriber
      This would also be very useful when importing local modules. I have defined some python functions in a module that I import. If I make any changes to these modules I cannot re-import then to the shell if they have already been imported. In order to re-import the new version of the module the shell needs to be restarted (I think). Thanks!
      Update: I found the reload module command in Python 2 which re-loads locally imported modules.
Viewing 1 reply thread
  • The topic ‘Clearing the Python shell namespace in Mechanical Scripting’ is closed to new replies.
[bingo_chatbox]