Welcome to the Harris Geospatial product documentation center. Here you will find reference guides, help documents, and product libraries.


Harris Geospatial / Docs Center / Using IDL / Python Bridge

IDL

Python Bridge

Python Bridge

Overview


The Python Bridge consists of two components: the IDL to Python bridge and the Python to IDL bridge. The bridge has the following features:

  • Works with Python 2 and Python 3
  • Access to all IDL routines and Python modules
  • Seamless: looks just like an IDL object or Python module
  • All bridge output is redirected to the standard output
  • Case sensitivity and row/column major is handled automatically
  • Can execute arbitrary command strings in either language
  • Automatic data conversion from IDL arrays to numpy arrays
  • Data is passed by reference when calling routines/methods
  • Can pass main variables back and forth

The Python Bridge also has a kernel for running IDL in an IPython notebook. See the IDL IPython Notebook Kernel for details.

Examples


For example, within IDL, you could execute the following Python commands to create a matplotlib plot:

IDL> ran = Python.Import('numpy.random')
IDL> arr = ran.rand(100)  ; call "rand" method
IDL> plt = Python.Import('matplotlib.pyplot')
IDL> p = plt.plot(arr)    ; call "plot", pass an array
IDL> void = plt.show(block=0)  ; pass keyword

Within IDL, you can also directly enter Python "command-line mode":

IDL> >>>
>>> import matplotlib.pyplot as plt
>>> import numpy.random as ran
>>> arr = ran.rand(100)
>>> p = plt.plot(arr)
>>> plt.show()
>>> 
IDL>

On the Python side, you can easily access all IDL functionality:

>>> from idlpy import *
>>> import numpy.random as ran
>>> arr = ran.rand(100)
>>> p = IDL.plot(arr, title='My Plot')
>>> p.color = 'red'
>>> p.save('myplot.pdf')
>>> p.close()

Installation


Your first step should be to install a working copy of Python 2 or Python 3 including the numpy library. The simplest method is to install one of the pre-configured installations such as Anaconda, which contains everything needed for the IDL-Python bridge.

Note: It is recommended that you install Python in a directory without any spaces or special characters.

To verify your Python installation, try the following Python commands:

>>>import sys
>>>sys.version
'3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]'
>>>import numpy as np
>>>np.array([1.0,2,3]).dtype
dtype('float64')

The next step is to configure your system so that IDL and Python can find each other.

Windows Platforms

You should ensure that your Python executable is on the Windows system PATH environment variable. IDL will use the first Python executable that it finds on the system path. You should also ensure that IDL's bin directory is on the Windows system PATH environment variable when launching Python. For example, for Anaconda on 64-bit Windows this would look like:

PATH = C:\Anaconda3;        
  C:\Anaconda3\Scripts;        
  C:\Anaconda3\Library\bin;        
  c:\Program Files\Harris\IDLXX\bin\bin.x86_64        

where XX is the IDL version number.

Note: If you want multiple versions of IDL side-by-side on your Windows system, you should not set this permanently. Instead, you should create a script to append the correct IDL bin directory to the PATH environment variable, then launch Python.

WinPython

If you are using WinPython instead of Anaconda, you will need to add the WinPython directories to PATH and PYTHONPATH. For example:

PATH = C:\WinPython-64bit-3.4.4.3\python-3.4.4.amd64\Scripts;        
C:\WinPython-64bit-3.4.4.3\python-3.4.4.amd64;        
C:\WinPython-64bit-3.4.4.3;        
C:\WinPython-64bit-3.4.4.3\scripts;        
c:\Program Files\Harris\IDLXX\bin\bin.x86_64        

For WinPython you will also need to define the PYTHONHOME environment variable. For example:

PYTHONHOME = C:\WinPython-64bit-3.4.4.3\python-3.4.4.amd64        

Linux Platforms

You should ensure that your Python executable is on the PATH environment variable. IDL will use the first Python executable that it finds on the system path. Then, you need to add the IDL bin directory and the Python lib directory to LD_LIBRARY_PATH.

For example, assuming that you have installed Anaconda in /usr/local/anaconda3, your .login file would look like:

setenv PATH /usr/local/anaconda3:${PATH}        
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/usr/local/anaconda3/linux_34x/lib:/usr/local/idl/bin/bin.linux.x86_64

As a last step, when using IDL from within Python, you should also ensure that IDL can find its fonts and resources by executing "source idl_setup" within IDL's bin directory.

Macintosh Platforms

You should ensure that your Python executable is on the PATH environment variable. IDL will use the first Python executable that it finds on the system path. In addition, you should set the PYTHONHOME variable to point to your Python installation.

For example, assume you have installed Anaconda in /Users/<user-name>/anaconda3. If you are using the C shell, your .login file would look like:

setenv PATH /Users/<user-name>/anaconda3/bin:${PATH}
setenv PYTHONHOME /Users/<user-name>/anaconda3

Note: To avoid conflicts with IDL widgets, you should not add these variables to the .cshrc file.

If you are using the Bash shell instead of the C shell, your variables would look like:

export PATH="/Users/<user-name>/anaconda3/bin:$PATH"
export PYTHONHOME="/Users/<user-name>/anaconda3"

In order for the IDL Workbench to pick up these environment variables, you need to start it from a command line that has these variables set.

As a last step, when using IDL from within Python, you should also ensure that IDL can find its fonts and resources by executing "source idl_setup" within IDL's bin directory.

Python Setup Script - All Platforms

The final step is to run the setup.py install script for the IDL Python bridge. Using either the Windows "cmd" window or a Unix shell, navigate to the <IDL_DIR>/lib/bridges directory and then run the script.

Note: Be sure that you run setup.py using the same Python that you wish to use for the IDL Python bridge.

For example, on Windows, use the Start menu to run "cmd.exe", then type:

cd c:\Program Files\Harris\IDLxx\lib\bridges
python setup.py install

where "xx" is your current IDL version.

On Mac or Linux, within a terminal window:

cd /usr/local/harris/idlxx/lib/bridges
python setup.py install

This script will install a file called idl_python.pth within your Python's site-packages folder. This file contains the paths to IDL's bin directory and the lib/bridges directory.

In addition, on the Macintosh platform, this script uses install_name_tool to modify the IDL Python bridge libraries to point to your Python installation.

Note: Depending upon who installed IDL and the installation location, you may need to run the setup.py script as an Administrator on Windows or as a "super-user" (sudo) on Mac or Linux. If the script fails with an error about permissions, either re-run the script using these elevated privileges or contact your System Administrator and have them run the script.

Troubleshooting Your Installation

If you are having problems starting the Python-to-IDL bridge:

>>>from idlpy import *
ImportError: No module named idlpy

Be sure you have run the setup.py install script (see above). This script sets up the paths within Python so it can locate your IDL installation. You can verify this from within Python:

>>>import sys
>>>sys.path

Python should print something similar to:

['',
'C:\\Anaconda3\\Scripts',
'C:\\<your IDL install dir>\\lib\\bridges',
'c:\\<your IDL install dir>\\bin\\bin.<your architecture>',
'C:\\Anaconda3\\python36.zip',
...

where <your IDL install dir> contains the full path to your IDL installation directory, and the bin.<your architecture> should match your machine architecture, for example bin.x86_64.

Tip: If you have multiple Python installations on your machine, you may need to set the PYTHONHOME environment variable to point to your desired Python.

Version History


8.5

Introduced

8.6

Added support for Python 3.5, added setup.py script

8.6.1

Added support for Python 3.6



© 2017 Exelis Visual Information Solutions, Inc. |  Legal
My Account    |    Buy    |    Contact Us