VERIFIED SOLUTION i

Understanding Python Support in Mapinfo Pro 17.0.3

Products affected: MapInfo Pro™
From the MapInfo Pro 17.0.3 Release Notes:
https://www.pitneybowes.com/content/dam/support/software/product-documentation/public/mapinfo-pro/v17-0-3/en-us/mapinfo-pro-v17-0-3-release-notes.pdf


MapInfo Pro version 17.0.3
MapInfo Pro Python Add-in
MapInfo Pro now support writing add-ins in Python language. This allows developers to take advantage of the Python language and its existing libraries/modules when writing MapInfo Pro add-ins. Python, with its feature rich set of libraries, is a very popular platform for Data Analysis and Visualization. We have provided Python developers and data analysts an easier way to work with MapInfo data by providing access to MapInfo Pro table data. While you can make use of any of the libraries and functionality available to Python, the ability to perform data analysis on MapInfo Pro table attribute data using the rich set of available data analysis tools (for example, pandas, SciPy, matplotlib, etc.) adds more capabilities and functionalities to existing ones.

Setting Up MapInfo Pro Python Add-in
Python Setup
1. Get latest 64-bit installer of Python version 3.7 version from here
Note: Make sure to use the Windows x86-64 executable installer under Downloads.
2. Run the installer as an Admin and choose Custom Install.
3. Make sure you are installing for ALL Users (Install Directory will default to: C:\ProgramFiles\Python37).
4. Check box for setting PATH variable and pre-compiled binaries.

Installing GDAL Python Bindings
1. Download the 64-bit Python installer.
2. Run the OSGeo4W setup program.
3. Select Advanced Install and press Next.
4. Select Install from Internet and press Next.
5. Select an installation directory. The default suggestion is fine in most cases. Press Next.
6. Select Local package directory. The suggestions is fine in most cases. Press Next.
7. Select Direct connection and press Next.
8. Choose download.osgeo.org and press Next.
9. Search GDAL, and install GDAL version.
10. If you have installed OSGeo4W in C:\OSGeo4W64:
• Then add C:\OSGeo4W64\bin to Path system environment variable.
• Define GDAL_DATA to C:\OSGeo4W64\share\gdal.
• Change paths as per your installation.
11. Open a command prompt window and execute "pip install --global-option=build_ext--global-option="-IC:\OSGeo4W64\include"--global-option="-LC:\OSGeo4W64\lib" gdal"
12. Close the command prompt window.
13. Open a new command prompt window.
14. Type Python and press Enter. This will open python interactive prompt.
15. Type "from osgeo import ogr".
This should work without error, verifying your GDAL python binding install.

Machine Environment Setup
Set the following two SYSTEM environment variables: to the Python installation directory (for example, C:\Program Files\Python37):
1. PYTHONPATH
2. PYTHONHOME

Setup Python Debugging using Visual Studio Code (VSCODE)
1. Install Python and pylint extension and restart VSCODE.
2. Initialize configurations. A configuration drives VS Code's behavior during a debugging session. Configurations are defined in a launch.json file that's stored in a .vscode folder in your workspace.
3. To initialize debug configurations, first select the Debug View in the sidebar.
Note: If you don't yet have any configurations defined, you'll see "No Configurations" in the drop-down list, and a dot on the settings icon:
4. To generate a launch.json file with Python configurations, do the following steps:
• Select the Settings button or use the Debug > Open configurations menu command.
• In the Select Environment drop-down list that appears, select Python.

The Python extension then creates and opens a launch.json file that contains number of pre-defined configurations. You can modify configurations (to add arguments, for example), and also add custom configurations.
User-added image

5. After the initial configuration is done, setup VSCODE for remote debugging.
• Install ptvsd using python -m pip install --upgrade ptvsd into your environment.
• Default Port for that ptvsd use for debugging is 5678. You may need to open the port for debugging in the appropriate firewall or other networking configuration.
After the above setup is done VSCODE can be used for writing and debugging python add-ins.
For more details refer to VSCODE python debugging :

Related Articles
• Python Tutorial: https://docs.python.org/3.7/tutorial/index.html
• Python Documentation: https://docs.python.org/3.7/index.html

Using the MapInfo Pro Python Add-in

Creating MapInfo Pro Python add-in

To create a MapInfo Pro add-in in python:
1. Set up your development and debugging environment, Setting Up MapInfo Pro Python Add-in on page 3
2. Use one of the existing python add-in templates to start with your add-in. The MapBasic 17.0.3 installation contains two python add-in templates for creating a simple MapInfo Pro add-in or a layout custom frame add-in in python.
• SAMPLES\RIBBONINTERFACE\Python\py_addin_templates\Simple
• SAMPLES\RIBBONINTERFACE\Python\py_addin_templates\CustomFrame
3. Copy one of the above add-in template and start modifying it. Start with renaming the files (PY and MB) to your add-in name.
4. Rename module reference as per your new file names.
Note: The PY file contains TODO comments to help you modify the code as per your requirement.
5. To load the add-in into MapInfo Pro, build the MB file using MapBasic 17.0.3 and run the MBX.
Note: Name of the MapBasic MB file should be same as the main python module file.
Note: The class below should be present in main python module file to load the add-in.

User-added image
User-added image

Debugging the Python Add-in in VSCODE
1. In VSCODE, open the folder of your python add-in or any python add-in sample (example : .\SAMPLES\RIBBONINTERFACE\Python\HelloPython)
2. Compile the add-in *.mbx using MapBasic IDE or Notepad++, etc.
3. In your python add-in source code, add the following lines.

User-added image

Note: In case you are debugging an existing sample, uncomment the code snippet above.
4. Run the add-in MBX in MapInfo Pro v17.0.3 or above. Now once MBX is run in MapInfo Pro, you will notice a wait cursor denoting that the process in waiting for some debugger to attach to it.
5. Switch to Debug View in VS Code, select Python: Attach configuration.
6. Set a breakpoint in the code where you want to start debugging.
Note: Setting a single breakpoint on the statement immediately following the ptvsd.wait_for_attach() line may not work. Set at least one other breakpoint on another statement.
7. Start the VS Code debugger using the modified Python Attach configuration. VS Code should stop on your locally-set breakpoints, allowing you to step through the code, examine variables, and perform all other debugging actions. Expressions that you enter in the Debug Console are run on the remote computer as well.
8. During remote debugging, the debugging toolbar appears. On this toolbar, the disconnect button (Shift+F5 ) stops the debugger and allows the remote program to run to completion.

 
UPDATED:  June 10, 2019