Skip to content

Make New Python System

Connor Jakubik edited this page Jun 18, 2024 · 5 revisions
Simulation Development Workflow → Make a New Python System

Up to date for Platform 0.23.0

Written by Connor Jakubik

Prerequisites:


Make a New Python System

  1. Copy Compute_Server/packages/Template_Python/Template_Python.py (in the STPro install directory) and paste the copy somewhere.
  2. Rename the file.
  3. The boilerplate code that is at the top of the template python script needs to be retained exactly as-is. This is what it might look like:
# THIS COMMENT LINE SHOULD BE THE FIRST LINE OF THE FILE!
# DON'T CHANGE ANY OF THE BELOW; NECESSARY FOR JOINING SIMULATION
import os, sys, time, datetime, traceback
import spaceteams as st
def custom_exception_handler(exctype, value, tb):
    error_message = "".join(traceback.format_exception(exctype, value, tb))
    st.logger_fatal(error_message)
    exit(1)
sys.excepthook = custom_exception_handler
st.connect_to_sim(sys.argv)
import numpy as np
# DON'T CHANGE ANY OF THE ABOVE; NECESSARY FOR JOINING SIMULATION
################################################################
  1. Explanation of boilerplate code here:
    • It's very important that this code stays intact and unmodified, and that it's the first non-comment lines of code in your file. Otherwise, hard-to-debug issues may happen.
    • import spaceteams as st imports the Space Teams Python API module. This will only work with the python interpreter we bundle with Space Teams, or exactly matching versions.
    • custom_exception_handler - Because the python scripts are started in a new process, in order to see console output in the event of an error, we need to replace the default sys.excepthook error-reaction with our own version that uses the spaceteams module logger_fatal() function which properly outputs to the space teams logs for this python system instance. logger functions are, however, only functional while a sim is running / connected-to.
    • st.connect_to_sim connects the python process to the STPro sim.
    • import numpy as np tries importing numpy. If this fails, there is likely an issue in python setup, and the exception is logged to the log file.
  2. You can modify any lines below this boilerplate.

Next Steps

  1. Consult the Python API Docs for further functionality.