Bridges > Python Bridge

# Python Bridge

- Overview
- Examples
- Installation
- IDL to Python bridge
- Python to IDL bridge
- IDL IPython Notebook Kernel

## 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

## 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 IDL

>>> 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.7.x or 3.4.x 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. To verify your Python installation, try the following Python commands:

>>> import sys

>>> sys.version

'3.4.1 |Anaconda 2.1.0 (x86_64)| (default, Sep 10 2014, 17:24:09) \n[GCC 4.2.1 (Apple Inc. build 5577)]'

>>> import numpy as np

>>> np.array([1.0,2,3]).dtype

dtype('float64')

The next step is to configure your system environment variables 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 64-bit Windows this would look like:

PATH = ...;c:\Program Files\Exelis\IDLXX\bin\bin.x86_64;...

where `XX` is the IDL version number.

If you want multiple versions of IDL side-by-side on your Windows system, you cannot set this permanently in your Windows environment. You need to create a script to append the IDL `bin` directory to the PATH environment variable, then launch Python.

You will also need to add IDL's `bin` directory and the `lib/bridges` directory to the PYTHONPATH environment variable. For 64-bit Windows this would look like:

PYTHONPATH = c:\Program Files\Exelis\IDLXX\bin\bin.x86_64; C:\Program Files\Exelis\IDLXX\lib\bridges

where `XX` is the IDL version number.

### 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. For example, assuming that you have installed Anaconda in /usr/local/anaconda3:

PATH = ...:/usr/local/anaconda3:...

You will also need to add IDL's bin directory and the lib/bridges directory to the PYTHONPATH environment variable. For example:

PYTHONPATH = /usr/local/idl/bin/bin.linux.x86_64:/usr/local/idl/lib/bridges

Then, you need to add both IDL and the Python lib directory to LD_LIBRARY_PATH. For example:

LD_LIBRARY_PATH = /usr/local/anaconda3/linux_34x/lib:/usr/local/idl/bin/bin.linux.x86_64

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

### Macintosh Platforms

To run the IDL-Python bridge on Macintosh platforms, you must set four environment variables:

Variable | PATH
Description | IDL will use the first Python executable that it finds on the system path. Therefore, prepend the Python `bin` directory to the current path.
Sequence of Directories | <python-bin-directory> $PATH
Bash Example | export PATH="/Users/<username>/anaconda3/bin:$PATH"

Variable | DYLD_LIBRARY_PATH
Description | Prepend the Python and IDL `lib` directories to the library path so that IDL can find the Python and IDL libraries. Due to library conflicts between Anaconda and Mac OS X, the `ImageIO` and `usr/lib` directories must precede the Python libraries.
Sequence of Directories | $DYLD_LIBRARY_PATH /System/Library/Frameworks/ImageIO.framework/Resources /usr/lib <python-lib-directory> <IDL_DIR>/bin/bin.darwin.x86_64
Bash Example | DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/System/Library/Frameworks/ImageIO.framework/Resources" DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/lib" DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/Users/<username>/anaconda3/lib" DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/Users/<username>/Applications/exelis/idl85/bin/bin.darwin.x86_64" export DYLD_LIBRARY_PATH

Variable | PYTHONHOME
Description | Specify the Python home directory.
Bash Example | export PYTHONHOME="/Users/<username>/anaconda3"

Variable | PYTHONPATH
Description | Prepend the IDL and `bridges` directories to the Python path.
Sequence of Directories | $PYTHONPATH <IDL_DIR>/bin/bin.darwin.x86_64 <IDL_DIR>/lib/bridges
Bash Example | export PYTHONPATH="$PYTHONPATH:/Users/<username>/Applications/exelis/idl85/bin/bin.darwin.x86_64:/Users/<username>/Applications/exelis/idl85/lib/bridges"

Set these variables as needed for your shell (Bash, C, Korn, etc.) 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, ensure that IDL can finds its fonts and resources by executing the following command within the IDL `bin` directory:

source idl_setup