MapInfo Pro Developers User Group

 View Only
  • 1.  Python from console vs called from Ribbon

    Posted 06-18-2025 12:51

    I have a couple simple scripts trying to iterate processes over directories. They work perfectly fine from the Python Console within MapInfo, however I cannot get them to run properly when called from a tool button. What extra steps or details are needed to get this working?

    import os
    from os.path import abspath, join
    import tkinter as tk
    from tkinter import filedialog
    import traceback
    
    from MapInfo.Types.Data import *
    
    try:
    
        root = tk.Tk()
        root.withdraw()
        
        dir_path = filedialog.askdirectory(title="Select a Folder")
        
        for file in os.listdir(dir_path):
            if file.endswith(".TAB"):
                tablePath = join(dir_path, file)
        
                pro.Catalog.OpenTable(tablePath)
                tabName = pro.Catalog.Tables[pro.Catalog.Tables.Count-1].Alias 
        
                do("Pack Table {} Data".format(tabName))  
        
                pro.Catalog.CloseAllTables()
    
    except Exception:
        traceback.print_exc()

    The call to the script is just as follows: 

    Sub Directory_Pack()
    	Run Program """C:\Program Files\MapInfo\Professional\Python310\python.exe"" ""C:\LM_Toolbox_2.0\Python\Directory_Pack.py"""
    End Sub



    ------------------------------
    Brandon Shepherd
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------


  • 2.  RE: Python from console vs called from Ribbon

    Posted 06-18-2025 13:33

    To be clear, we have other tools that work successfully using this call method, but none that attempt to use the MapInfo Pro Extensibility to use internal features. I believe the issue may stem from the MapInfo.Types Import outside of the Python Console. 

    Our success with Python Extensibility has been very limited. We want to create python add-ins down the line that do not require accompanying map basic scripts, but are so far hung up on the method. 



    ------------------------------
    Brandon Shepherd
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------



  • 3.  RE: Python from console vs called from Ribbon

    Employee
    Posted 06-19-2025 05:04

    Hey Brandon

    A Python script is considered an application or a script in MapInfo Pro.

    Can you try running the Python file using one of these statements:

    Sub Directory_Pack()
    	Run Application "C:\LM_Toolbox_2.0\Python\Directory_Pack.py"
    End Sub

    or

    Sub Directory_Pack()
    	Run Script "C:\LM_Toolbox_2.0\Python\Directory_Pack.py"
    End Sub



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 4.  RE: Python from console vs called from Ribbon

    Posted 06-19-2025 10:01

    Thanks Peter, Run Application did the trick. 



    ------------------------------
    Brandon Shepherd
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------