PyVISA on OSX

So after some persistance, I’ve managed to get PyVISA working under OS X 10.6.8 Snow Leopard on a late 2008 MacBook Pro.

  1. Download and install NI VISA 5.1.2 for OSX
  2. In your home directory, create the file .pyvisarc with the contents
    [Paths]
    VISA library: /Library/Frameworks/VISA.framework/VISA
  3. Download and untar PyVISA 1.4
  4. Install as root with easy_install
    $ sudo easy_install PyVISA-1.4

Now, NI VISA 5.1.2 was compiled for 32 bit systems – check this with:
$ file /Library/Frameworks/VISA.framework/VISA

If it returns i386, not x86_64, you’ve got a 32 bit binary. If you try to use PyVISA as shipped with OSX, it will default to 64 bit mode. When you try to import visa, you’ll get several errors reading no suitable image found and no matching architecture in universal wrapper.

This is a CTYPES problem and discussed in this stackoverflow question.

It is possible to force OSX to run Python in 32 bit mode by
$ export VERSIONER_PYTHON_PREFER_32_BIT=yes

Which you could put in your .profile or .bashrc to run every time you open a new shell. But I didn’t want to default to 32 bit python, I’d rather force it into 32 bit mode when needed.

So, to my .profile I added
alias python386='arch -i386 python'

To force a script to execute under 32 bit Python, rather than including the line #!/usr/bin/python, I start my script with #!/usr/bin/env arch -i386 python.

I’m still working on a way to force iPython to run in 32 bit mode, if started with arch it still calls system python. Most unfortunately, I do not have 32 bit versions of many useful packages (e.g., numpy and pylab) forcing me to save data (say as a CSV file) and process it with 64 bit Python). That will become really tiresome if I try to be interactive with a PyVISA.

I specified the version numbers that I used, but am unsure if they are required, just what worked for me.

Below, I’ve included a short demo, verifying proper operation of the HP/Agilent 34401A.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env arch -i386 python
# pyvisa calls NI visa which is compiled with 32 bits, so run python in 32 bit
from visa import *
 
inst = SerialInstrument('ASRL1',
                        baud_rate = 9600,
                        data_bits = 7,
                        stop_bits = 2,
                        parity = even_parity,
                        term_chars = CR+LF,
                        end_input = term_chars_end_input,
                        send_end = True,
                        delay = 2
                        )
 
inst.write('SYST:REM')
inst.write('*RST')
sleep(10)
inst.write('SYST:REM')
print inst.ask('*IDN?')

It should print HEWLETT-PACKARD,34401A,0,11-5-2, or whatever your software version is.



3 Comments

  1. Vivek wrote:

    This is great….I have a question:

    Is there way to constantly print, save the data without giving it a length of data to save. What I want to do is always print the data via inst.ask () and once I know I have enough data I tell it to stop.

  2. […] wrote a little previously about setting up PyVISA on OSX, but didn’t show any real examples of its use. Here, as a proof of concept, I measure the […]

Leave a Reply