Having Trouble Connecting to STK with Python?

Information

 
QuestionHaving Trouble Connecting to STK with Python?
Answer
This is step 4 in the "Troubleshooting your STK and Python integration" FAQ. Please check out this FAQ first before running through these steps to get your Python working with STK.

Note: An STK Integration license is required to connect to STK with Python.
Note: Python 3.7.6 does not work with comtypes and therefore you cannot connect to STK with comtypes on this version

If you are experiencing an error when running trying to connect to STK:
 
import comtypes
from comtypes.client import CreateObject
app = CreateObject('STK11.Application')  #  Possible error
app.Visible = True
root = app.Personality2 # Possible error


there is probably an issue with the Python wrapper files located in the comtypes gen folder.
Run the following code to determine your comtypes gen folder:
 
import comtypes.client
print(comtypes.client.gen_dir, '\n')

Copy the file path into the address bar of a File Explorer window (which you can access with the folder icon on the task bar). You will see multiple files; these are the files that allow Python to talk to STK (as depicted below).

User-added image
Figure 1. Python wrapper files, seen in Windows File Explorer
 
If you cannot connect to STK, some of the files - those with a filename that contains a GUID (which look like a random sequence of numbers and letters) - may not have been generated properly and are probably empty. You can verify if this is the problem by opening the files in a text editor and reviewing the contents. Regenerate any files that are empty. Note: in general, these files will always have to be regenerated between major releases (e.g., STK 10, STK 11, etc.) of STK.
  1. To begin regenerating any empty files, delete all the contents in this folder.
  2. Then, rerun:
import comtypes
from comtypes.client import CreateObject
app = CreateObject('STK11.Application')  #  Possible error
app.Visible = True
root = app.Personality2 # Possible error
  1. When "app = CreateObject('STK11.Application')" and "root=uiApplication.Personality2" are executed, the comtypes library automatically creates the gen folder that contains the Python wrappers for the STK libraries.
  2. You should see multiple outputs saying "# Generating comtypes.gen. xxxxx", and the contents of the gen folder will be populated.
  3. If you are trying to access the Astrogator library, specifically, then you will also need to run the following line:
comtypes.client.GetModule((comtypes.GUID("{090D317C-31A7-4AF7-89CD-25FE18F4017C}") ,1,0))


User-added image
  1. Once the wrapper files have been generated correctly, they can be imported directly:
from comtypes.gen import STKObjects
from comtypes.gen import STKUtil
from comtypes.gen import AgSTKVgtLib
from comtypes.gen import AgSTKGraphicsLib
from comtypes.gen import AgStkGatorLib
from comtypes.gen import AgUiApplicationLib
from comtypes.gen import AgUiCoreLib

If you still cannot connect to STK double check the permissions of the gen folder and make sure the Properties->Security is Full Control. If trying to connect to still doesn't work, you can download the attached zip folder for STK 11, extract the contents, and paste them into your comtypes gen folder.
 
Resource File 1
Resource File 2