Running Python Scripts and Add-ins - MapInfo_Pro - 2023

MapInfo Pro Help

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapInfo Pro
Version
2023
ft:locale
en-US
Product name
MapInfo Pro
ft:title
MapInfo Pro Help
First publish date
1985
ft:lastEdition
2023-09-12
ft:lastPublication
2023-09-12T16:39:16.995000

The "run application" MapBasic statement now supports running *.py files. Any valid python will work as long as the required modules are installed. The Run Program dialog (Ctrl+U) now lists both .mbx and .py files. You can also drag a .py file onto MapInfo Pro to run it.

There are two ways in which MapInfo Pro runs Python files:
  1. Add-in Scope - If the .py file is recognized as a MapInfo Pro add-in, then it is run in its own scope and app domain. Note that when the add-in is run, a .mbx file will still be created next to it, which means that the location must be write-able.
  2. Global Scope - If the .py file is not recognized as a MapInfo Pro add-in then it is run in the Global scope. This is a special python environment that is initialized the first time any python code is run. The following setup is done:
    • sys, clr, subprocess, shlex, matplotlib python modules are already imported in this special python environment.
    • matplotlib module uses a Non-GUI backend called agg which will not allow call to plt.show() from within MapInfo Pro in global scope.
    • Reference to WindowsBase .Net framework assembly is added using clr module.
    • PythonProUtil c# utility class is imported as proUtil from MapInfo.Types assembly.
    • MessageOutput class is imported from MapInfo.Types assembly.
    • MessageOutput class overrides the default python stdout, stdin, stderr and prints to MapInfo Pro message window.
      import clr
      import subprocess
      import shlex
      import matplotlib
      # Non-GUI backend for matplotlib. use plt.savefig(fname) instead of plt.show()
      matplotlib.use('agg')
      clr.AddReference('WindowsBase')
      from MapInfo.Types import PythonProUtil as proUtil
      from MapInfo.Types import MessageOutput
      sys.stdout = sys.stderr = sys.stdin = MessageOutput()
    • RASTER folder is added to path.
    • A python variable named pro is initialized to point to the IMapInfoPro interface. It can be used to access MapInfo Pro's object model and to execute MapBasic commands.
    • The following help methods are imported in global scope, which can be used directly in Python scripts:
      end_mapinfo(interactive) -> Halts MapInfo Pro.
          Parameters:
          interactive (bool): prompt to save changes or not when exiting.
      
      do(command) -> Execute an interpreted MapBasic command.
          Parameters:
          command (str): the command to run.
      
      eval(command) -> Evaluate an interpreted MapBasic function.
          Parameters:
          command (str): the command to evaluate.
      
      get_current_module_path() -> Gets the current executing module path 
      for standalone python script.(In global scope __file__ is not available, 
      therefore use this method.)
    • If a startup.py file is found in one of the following locations it is executed in the global scope setup. Similar to the way startup.wor is found.
      • FOLDER_MI_APPDATA (-1)
      • FOLDER_MI_LOCAL_APPDATA (-2)
      • FOLDER_MI_PREFERENCE (-3)
      • FOLDER_MI_COMMON_APPDATA (-4)
      • Folder where MapInfoPro.exe is located

See GetFolderPath$() MapBasic function for more help on these locations.

Once the global scope has been initialized as above, every time the Python code is run, a copy of the global scope is made and used to execute the code.