MapInfo Pro

Expand all | Collapse all

Getting Started using Python In MapInfo Pro

  • 1.  Getting Started using Python In MapInfo Pro

    Posted 04-29-2020 13:41
    Here is some quick info on how to get started using Python in MapInfo Pro.

    Starting with v2019.1, Python is installed and ready to use out of the 'box'.

    See my post here for ideas on what you could do with python and why:
    https://community.pitneybowes.com/communities/community-home/digestviewer/viewthread?GroupId=19&MessageKey=72f33381-f319-497b-9b74-ee6a6c906a7d&CommunityKey=fe4d1ee2-f1c6-4307-87d2-00bea1344885&tab=digestviewer&ReturnUrl=%2fcommunities%2fcommunity-home%2fdigestviewer%3fcommunitykey%3dfe4d1ee2-f1c6-4307-87d2-00bea1344885%26tab%3ddigestviewer

    First, you will want to get two Python help applications from the MapInfo Marketplace.
    The easiest way to do that is to type 'python' into the quicksearch toolbar. (It now also searches the Marketplace.)
    Type Ctrl+Q then type python:


    You will see two Python applications:

    Python Console

    You can use any Python IDE to develop your code, but we are providing a small Python Console via the MapInfo Marketplace to help with interactively editing and running small python scripts. It supports syntax highlighting and intellisense.

    Python Quick Start
    This add-in has many python code examples from simple to complex.
    In addition, in includes reference materials and links to web sites and documentation to help you get going.

    Click on the search results and the Marketplace window will open in Pro. Simply click the Install Product button.

    Then do the same for the Python Quick Start add-in.

    Here is a screen shot of the reference page:

    There are over 30 examples in various categories, and you can search for specific text like a mapbasic or python keyword you want to see how to use.

    Here is one example that tells Pro to convert a .csv file to a MapInfo Table:


    The edit button opens up the script into the Python Console.


    The Run button executes the script.

    Feel free to come here with questions or ideas.

    Please give it a try!

    -Bob




    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    MapInfo Pro Development Team
    ------------------------------


  • 2.  RE: Getting Started using Python In MapInfo Pro

    Posted 9 days ago
    Hi Bob.

    Quick question, does MapInfo use the 3.7 version only?
    I'm asking as I already had 3.8 installed on my machine, and now I'm not sure if any of the samples are running correctly.

    Do I need to have all the libs in 3.7 folder? or is there a way for MapInfo to recognize the current version installed and subsequent Libs ?

    ------------------------------
    Maria Sousa
    WOOLWORTHS GROUP LTD
    Bella Vista NSW
    ------------------------------



  • 3.  RE: Getting Started using Python In MapInfo Pro

    Posted 8 days ago
    Hi Maria,

    Yes Pro uses and requires Python 3.7. We install everything that is needed along with many Python modules.

    Here is some info from 2019.1 release notes. I hope it is not too much information :-) but feel free to ask any followup questions.

    -Bob

    Installation:

    Starting from version 2019.1 MapInfo Pro x64 and MapInfo Runtime x64 Python 3.7.6 x64 will be installed with product in installation directory under Python37 folder. 

    Pro uses the PythonNet implementation to interact with the Python environment and allow interop between .net and python.

    Python installation bundled within the product will have the below modules pre-loaded.

    1. numpy
    2. scipy
    3. seaborn
    4. pandas
    5. matplotlib
    6. osgeo (Access to GDAL library with MapInfo EFAL driver for NativeX format)
    7. ptvsd (Python Tools for Visual Studio debug server)
    8. mi_py_debugger (Custom module enable debugging of python script in MapInfo Pro)
    9. PyQT5
    10. PyWin32

    Python and the modules are installed in a subfolder under the Pro installation folder named PYTHON37

    EFAL is installed in a subfolder under the Pro installation folder named EFAL

    Install Additional Python modules:

    To install additional python modules user need to open the script prompt.bat in %INSTALLDIR%\Python37 folder and then execute the below command.

    python -m pip install <module_name>

    Python Environment Variables and Initialization (Internal only)

    During the initialization of Python within MapInfo Pro, below environment variables are defined in the executing MapInfo Pro process for correctly loading shipped python37.

    PROHOME → Path to current executing MapInfo Pro executable.

    PROPYTHONHOME → Path to %INSTALLDIR%\Python37 folder shipped with MapInfo Pro installation.

    PATH → Below paths added to PATH environment variable.

    • Path to %INSTALLDIR%\Python37 folder shipped with MapInfo Pro installation.
    • Path to %INSTALLDIR%\Python37\Scripts folder shipped with MapInfo Pro installation.
    • Path to %INSTALLDIR%\EFAL folder shipped with MapInfo Pro installation.

    Running Python Scripts and Add-ins in MapInfo Pro

    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 list both .mbx and .py files.

    In addition you can drag a .py file onto Pro to run it.

    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)

    MapInfoPro.exe location

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

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



    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    MapInfo Pro Development Team
    ------------------------------