PyVISA Quickstart: Control Your First Instrument in 5 Minutes

PyVISA Setup · Beginner · ~10 min

Install PyVISA, connect to an instrument over USB or LAN, send your first SCPI command, and read back a measurement — all in one guide.

Prerequisites

You'll need

  • Python 3.7 or later installed (python.org)
  • A VISA-compatible instrument connected via USB, LAN, or GPIB
  • A terminal or command prompt

PyVISA is a Python library that lets you control test and measurement instruments using the VISA (Virtual Instrument Software Architecture) standard. It works with instruments from Keysight, Tektronix, Rigol, Rohde & Schwarz, and many others.

Step 1

Install PyVISA

PyVISA requires a VISA backend to communicate with instruments. The easiest option for beginners is pyvisa-py, a pure-Python backend that works without installing a vendor VISA library.

pip install pyvisa pyvisa-py

If you already have NI-VISA installed (from National Instruments), PyVISA will detect and use it automatically — you don't need pyvisa-py in that case. NI-VISA is required for GPIB instruments on most systems.

Which backend should I use? Use pyvisa-py for USB and LAN instruments — no extra installation required. Use NI-VISA if you need GPIB support or are in a production environment that already has it deployed.

Verify the installation:

python -c "import pyvisa; print(pyvisa.__version__)"
Step 2

Find Your Instrument Address

Every VISA instrument has a resource string that uniquely identifies it. Run the following snippet to list all instruments currently connected to your computer:

import pyvisa

rm = pyvisa.ResourceManager()
print(rm.list_resources())

You'll see output like one of these:

Connection Example VISA Resource String
USB USB0::0x2A8D::0x1301::MY12345678::INSTR
LAN (VXI-11) TCPIP0::192.168.1.100::inst0::INSTR
LAN (raw socket) TCPIP0::192.168.1.100::5025::SOCKET
GPIB GPIB0::22::INSTR
Serial (COM/tty) ASRL3::INSTR (Windows) / ASRL/dev/ttyUSB0::INSTR
Nothing listed? If list_resources() returns an empty tuple, jump to the Troubleshooting section below.

Copy the resource string for your instrument — you'll use it in the next step.

Step 3

Connect to the Instrument

Open a connection using open_resource() with the resource string you found:

import pyvisa

rm = pyvisa.ResourceManager()

# Replace with your instrument's resource string
inst = rm.open_resource('USB0::0x2A8D::0x1301::MY12345678::INSTR')

# Verify the connection — every SCPI instrument must respond to *IDN?
print(inst.query('*IDN?'))

The *IDN? (identification query) is a mandatory SCPI command. A successful response looks like:

Keysight Technologies,34461A,MY12345678,A.02.17-02.40-02.17-00.49-03-01

If you see this, you're connected. The response contains manufacturer, model, serial number, and firmware version.

Step 4

Send Commands & Read Measurements

PyVISA provides three core methods for communicating with instruments:

# query() — sends a command and reads the response (for ? commands)
response = inst.query('MEAS:VOLT:DC?')
print(f"DC Voltage: {response.strip()} V")

# write() — sends a command with no response expected
inst.write('CONF:VOLT:DC')       # configure for DC voltage measurement
inst.write('SENS:VOLT:DC:RANG 10')  # set range to 10 V

# read() — reads pending data after a write()
inst.write('READ?')
value = inst.read()
print(f"Reading: {value.strip()}")
query() vs write() + read()
Use query(cmd) as shorthand for write(cmd) followed by read(). They are equivalent. Most code uses query() for commands ending in ? and write() for configuration commands.

SCPI commands follow a hierarchical tree structure separated by colons. MEAS:VOLT:DC? means: MEASure → VOLTage → DC (with ? making it a query). You can find all valid commands for your instrument in the SCPI Command Reference.

Step 5

Full Example & Clean Up

Here's a complete script that connects to a DMM, takes three DC voltage readings, and closes the connection cleanly:

import pyvisa

# Open the resource manager
rm = pyvisa.ResourceManager()

# Connect to the instrument (update this string for your device)
inst = rm.open_resource('USB0::0x2A8D::0x1301::MY12345678::INSTR')
inst.timeout = 5000  # 5 second timeout (milliseconds)

try:
    # Identify the instrument
    idn = inst.query('*IDN?').strip()
    print(f"Connected: {idn}\n")

    # Reset to known state
    inst.write('*RST')

    # Configure for DC voltage measurement
    inst.write('CONF:VOLT:DC 10')      # 10 V range
    inst.write('SENS:VOLT:DC:NPLC 10') # 10 PLC integration (slower but more accurate)

    # Take 3 readings
    for i in range(3):
        reading = float(inst.query('READ?'))
        print(f"Reading {i+1}: {reading:.6f} V")

finally:
    # Always close the connection
    inst.close()
    rm.close()
Always use try/finally. Wrapping your code in a try/finally block ensures the instrument connection is closed even if an error occurs. Leaving connections open can prevent other applications from accessing the instrument.

Troubleshooting

list_resources() returns an empty tuple

The pyvisa-py backend may not detect all instrument types automatically. Try specifying the backend explicitly:

rm = pyvisa.ResourceManager('@py')  # force pyvisa-py backend
print(rm.list_resources())

For USB instruments, also install pyusb:

pip install pyusb

Permission denied on Linux/macOS (USB)

USB instrument access on Linux requires a udev rule. Create the file /etc/udev/rules.d/99-visa.rules with:

SUBSYSTEM=="usb", MODE="0666", GROUP="plugdev"

Then reload: sudo udevadm control --reload-rules and reconnect the instrument.

GPIB instruments not found

pyvisa-py has limited GPIB support. For GPIB you need either NI-VISA (Windows/Linux/macOS) or linux-gpib (Linux only). See the GPIB Setup Guide for step-by-step driver installation.

Timeout error during query()

Increase the timeout (in milliseconds) for slow instruments or long measurements:

inst.timeout = 10000  # 10 seconds

VisaIOError: VI_ERROR_RSRC_NFOUND

The resource string is wrong or the instrument is not powered on. Double-check with rm.list_resources() and copy the string exactly.

Next Steps

Now that you can communicate with an instrument, explore these resources: