Using a Standard Python Interpreter

Here are some notes about using the VisIt Python API with a standard Python interpreter (not the VisIt cli that comes with VisIt).

Note that you will probably want to invoke Python in interactive mode (using'-i') when using your script.

Telling Python where to find the VisIt module

In order for Python to load VisIt's Python interface, it must know where to find visitmodule.so. There are different ways to do this but the least invasive is to append to sys.path.

Append to sys.path

The sys.path list contains the paths that Python will search when looking for modules. To make sure that VisIt's Python module is located when we do "import visit" or "from visit import *", we need to append the path to the visit module. This can be done using the following code:

import sys
sys.path.append("/usr/local/visit/current/linux-x86_64/lib/site-packages")
import visit
visit.Launch()

Note the use of linux-x86_64 in the path. You may need to use linux-intel, darwin-x86_64, or darwin-i386, depending on your platform.

Launch

If you are developing VisIt then you will need to also pass the path to VisIt's bin directory to the Launch() method to ensure that your visitmodule.so gets loaded. If you do not pass the path to the bin directory then the visit -env that the VisIt module executes under the covers will find the installed VisIt and the wrong visitmodule.so could be launched.

visit.Launch(vdir="/my/private/visit/2.5RC_trunk/src/bin")

Set PYTHONPATH

The PYTHONPATH environment variable provides another way to inform Python of the location of the visit module. On 64-bit Linux this value could be: /usr/local/visit/current/linux-x86_64/lib/site-packages.

Consult the documentation for your command shell if you need help doing this. Common ways are:

1.

env PYTHONPATH=/usr/local/visit/current/linux-x86_64/lib/site-packages python

2.

setenv PYTHONPATH /usr/local/visit/current/linux-x86_64/lib/site-packages
python

Version 2.4 and 2.5 Differences

Before version 2.5, VisIt's Python module consisted of a front end C extension module visit.so and a back end module visitmodule.so that contained the actual functionality. While a little unconventional, the purpose of having the front end module was to allow the front end module to selectively run different back end modules, effectively allowing different functionality to be imported into the Python module.

With VisIt 2.5.0, the front end module was rewritten in Python and refactored such that it could include other Python language sources into the module. This change was meant to simplify the addition of Python-only code into the VisIt module.

There have been a couple of reports of incompatibilities with existing scripts.

To use existing scripts with pre-2.5 versions of VisIt, you can a script like the following:

# Run using command line:
#
# python visit2.4.2.py
#
import sys
sys.path.append("/usr/gapps/visit/2.4.2/linux-x86_64/lib")

import visit
visit.AddArgument("-v")
visit.AddArgument("2.4.2")
visit.Launch()

visit.OpenDatabase("/usr/gapps/visit/data/noise.silo")
visit.AddPlot("Pseudocolor", "hardyglobal")
visit.DrawPlots()
visit.SaveWindow()

To use the new Python module, you'll have to make a few changes to your script:

  1. Add site-packages to the python path
  2. Call visit.ShowAllWindows() to make the viewer windows show up.

These changes stem from recent refactoring of the visit module.

# Run using command line:
#
# python visit2.5.2.py
#
import sys
sys.path.append("/usr/gapps/visit/2.5.2/linux-x86_64/lib/site-packages")

import visit
visit.Launch()
visit.ShowAllWindows()

visit.OpenDatabase("/usr/gapps/visit/data/noise.silo")
visit.AddPlot("Pseudocolor", "hardyglobal")
visit.DrawPlots()
visit.SaveWindow()

Importing VisIt's Module

Note: When you launch visit from visit's python module, the front end module actually launches the version of 'visit' that exists in your path. This may not match the version of the front end module.


There are 2 ways that you might want to import VisIt's symbols into Python. The first way adds the visit module to Python and requires you to add a visit. prefix to all function calls. The second way adds the visit module's symbols to the global Python namespace, mirroring what the VisIt CLI does.

# you could do this:
import visit
visit.Launch()

# or, you could do this:
from visit import *
Launch()

The following code snippets should allow your code to be used both with the VisIt CLI, and also with a 'native' Python interpreter.

See if you are in the CLI:

# Detect if running in CLI ('visit -cli') and if not check Python version
isCLI = True
if 'VisItException' not in dir():
     if sys.version_info[1] < 5:
         print "\nRequires Python version 2.5 or greater\n"
         sys.exit()
     isCLI=False

Note the above checks for the existence of a particular VisIt symbol and Python version and may need to be updated for later versions of VisIt.

Import the module, if required:

# Import visit into a standard Python interpreter, if required (get error
# if run Launch() from CLI). Launch() is required to start the Viewer and
# import the rest of the API. Do after parsing of args as it takes a while.
if not isCLI:
     from visit import *
     Launch()