''' ImportModuleWith_crt_Reference.py Last Modified: 23 Sep, 2021 - Modified the way the script unloads the module if it's already been imported so that the approach works across SecureCRT 9.0.x and 9.1.x versions, since a change in the way 9.1.x handles global script objects would result in an error when attempting to reload the module using the 'reload' method. Last Modified: 12 Aug, 2021 - Modified import/reload code to work in either python 2.7 or python 3.x Last Modified: 10 Oct, 2017 - Included 'sys.dont_write_bytecode' and 'reload(crt_module)' directives to assist individuals who may struggle with how a python interpreter works with imported modules. Last Modified: 01 May, 2013 - Original version DESCRIPTION: This example illustrates how to import a custom module that uses the crt object. This script requires the companion module called crt_Module.py. Here is the code of the companion script if you don't have it: ------------------------------------------------------------------------------- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def Inject_crt_Object(obj_crt_API): # Make the crt variable global for use in multiple functions in the # module that might need access to the crt object. global crt # Associate the crt variable with the crt object passed in from the # main script. crt = obj_crt_API return #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def Test_crt_Object(): # Test using the crt object. crt.Dialog.MessageBox("I'm a function in a python module!") return "It was the best of times, it was the worst of times" ------------------------------------------------------------------------------- ''' import sys, os # NOTE: A python interpreter will automatically create a .pyc file for # any custom module that you import. These .pyc files might cause # some individuals grief, as now they have confusion over .py and # .pyc files of the same basename hanging around inside their script # source folders. # # Use 'sys.dont_write_bytecode = True', to PREVENT python from # automatically creating these .PYC files: sys.dont_write_bytecode = True # sys.path is a variable that tells a script where to look for modules when # importing them. Here is an example of how to add a path you want to use if # you don't want to use a pre-defined path. # First, get the path to the script that is running, assuming the module to # be imported is in that same location: strScriptPath = os.path.dirname(__file__) # Next, inject the path to the script into sys.path, which is what python # will refer to when looking for modules to be import'd: if not strScriptPath in sys.path: # Inject the path of the running script if it is not in sys.path sys.path.insert(0, strScriptPath) # Check to see if we need to unload the crt_Module... why? Because # during the development/testing phase of writing the crt_Module # code, whenever we run our main script we want to always have a # "fresh" version of the module imported so that changes are reflected # without having to close/restart the SecureCRT process. if "crt_Module" in sys.modules: del sys.modules['crt_Module'] # Now, let's import our super special module that uses the 'crt' # object. import crt_Module # Now, pass the crt object to the imported module via this function # so it can be used throughout the imported module's code. crt_Module.Inject_crt_Object(crt) # Inside our Main() function here, we'll be calling a function that # is defined within the imported module: Test_crt_Object() # This function displays a message, and returns a value. The returned # value is then displayed in another message box. # #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def Main(): # Show that we can use the crt object that was passed into the module. strMessage = crt_Module.Test_crt_Object() # Show that we can still use the crt object in the main script. crt.Dialog.MessageBox(strMessage) return Main()