{"id":4735,"library":"pyvisa","title":"PyVISA","description":"PyVISA is a Python package that provides bindings to the \"Virtual Instrument Software Architecture\" (VISA) library. It enables control of various measurement devices and test equipment (e.g., oscilloscopes, multimeters, power supplies) through different interfaces like GPIB, RS232, USB, and Ethernet. It acts as a frontend to either a vendor-provided VISA shared library (like NI-VISA or Keysight IO Library Suite) or a pure Python implementation like PyVISA-py. The library is actively maintained, with the current version being 1.16.2, and receives regular updates.","status":"active","version":"1.16.2","language":"en","source_language":"en","source_url":"https://github.com/pyvisa/pyvisa","tags":["measurement","instrument-control","GPIB","RS232","USB","Ethernet","VISA","scientific","hardware-control"],"install":[{"cmd":"pip install -U pyvisa","lang":"bash","label":"Install PyVISA"},{"cmd":"pip install -U pyvisa-py","lang":"bash","label":"Install PyVISA-py backend (optional)"}],"dependencies":[{"reason":"PyVISA acts as a wrapper for an underlying VISA implementation. You must install a vendor-specific VISA library, or use pyvisa-py as a pure Python backend.","package":"VISA shared library (e.g., NI-VISA, Keysight IO Library Suite)","optional":false},{"reason":"Optional dependency for PyVISA-py to enable Serial (ASRL) resources.","package":"PySerial","optional":true},{"reason":"Optional dependency for PyVISA-py to enable USB (USB INSTR/RAW) resources. Requires underlying USB driver library like libusb.","package":"PyUSB","optional":true},{"reason":"Optional dependency for PyVISA-py to discover TCPIP (VXI-11) devices across multiple network interfaces.","package":"psutil","optional":true},{"reason":"Optional dependency for PyVISA-py to enable HiSLIP and VICP device discovery.","package":"zeroconf"}],"imports":[{"note":"While 'import visa' might work in older versions or specific setups, 'import pyvisa' is the canonical and recommended way. Similarly, ResourceManager is accessed as `pyvisa.ResourceManager()`.","wrong":"import visa","symbol":"ResourceManager","correct":"from pyvisa import ResourceManager"}],"quickstart":{"code":"import os\nimport pyvisa\n\n# For simulation or when no physical VISA library is installed, use the PyVISA-py backend\n# Otherwise, PyVISA will try to find a system-installed VISA library (e.g., NI-VISA)\n# You can also explicitly specify a backend or path: rm = pyvisa.ResourceManager('@py')\n# For a specific VISA library path: rm = pyvisa.ResourceManager('/path/to/visa.dll')\n\nrm = pyvisa.ResourceManager()\n\ntry:\n    # List available resources (instruments)\n    resources = rm.list_resources()\n    print(f\"Available resources: {resources}\")\n\n    if not resources:\n        print(\"No instruments found. Ensure your instruments are connected and the VISA backend is correctly configured (e.g., NI-VISA or PyVISA-py).\")\n    else:\n        # Open the first available instrument, or a specific one if known\n        # Replace 'YOUR_INSTRUMENT_ADDRESS' with an actual address from rm.list_resources()\n        # e.g., 'GPIB0::12::INSTR', 'ASRL1::INSTR', 'USB0::0xXXXX::0xXXXX::SN::INSTR', 'TCPIP0::192.168.1.100::INSTR'\n        # For this example, we'll try to connect to a dummy address if no real one is found\n        # In a real scenario, you would select an actual instrument from 'resources'\n        instrument_address = os.environ.get('PYVISA_INSTRUMENT_ADDRESS', resources[0] if resources else 'ASRL1::INSTR') # Fallback if no resources\n\n        if instrument_address.startswith('ASRL') and 'ASRL1::INSTR' in resources:\n             # Special handling for serial, often requires baud_rate\n             # This is a placeholder; actual baud_rate must match the instrument\n             inst = rm.open_resource(instrument_address, baud_rate=9600)\n             inst.write_termination = '\\n'\n             inst.read_termination = '\\n'\n        else:\n            inst = rm.open_resource(instrument_address)\n\n        # Query the instrument's identification string\n        idn = inst.query('*IDN?')\n        print(f\"Instrument ID: {idn.strip()}\")\n\n        # Example: Write a command and read a response\n        # inst.write('MEAS:VOLT:DC?') # Example SCPI command\n        # response = inst.read()\n        # print(f\"Measurement: {response.strip()}\")\n\n        inst.close()\n        print(\"Instrument connection closed.\")\n\nexcept pyvisa.VisaIOError as e:\n    print(f\"VISA I/O Error: {e}\")\n    print(\"Ensure the VISA backend is installed and configured, and the instrument address is correct.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize the ResourceManager, list available instruments, open a connection to an instrument, query its identification string, and then close the connection. It includes basic error handling and notes for serial configuration. Replace 'YOUR_INSTRUMENT_ADDRESS' with an actual resource string for your device. If no physical VISA backend is found, it automatically uses the PyVISA-py backend if installed."},"warnings":[{"fix":"Replace `pyvisa.instrument(\"ADDRESS\")` with `rm = pyvisa.ResourceManager(); inst = rm.open_resource(\"ADDRESS\")`. Replace `pyvisa.get_instruments_list()` with `rm.list_resources()`. Replace `inst.ask(\"CMD\")` with `inst.query(\"CMD\")`.","message":"API changes in PyVISA 1.6+ from versions < 1.5. The direct use of `pyvisa.instrument()` and `pyvisa.get_instruments_list()` methods are removed. The `ask()` method on an instrument object was renamed to `query()`.","severity":"breaking","affected_versions":"< 1.6"},{"fix":"Install a VISA runtime from a vendor (like National Instruments or Keysight) appropriate for your operating system, or install `pyvisa-py` via `pip install pyvisa-py`.","message":"PyVISA requires an underlying VISA implementation. It does not provide the VISA library itself. You must install a vendor's VISA library (e.g., NI-VISA, Keysight IO Library Suite) or the pure Python backend, PyVISA-py.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure that your Python installation (32-bit or 64-bit) matches the bitness of the installed VISA shared library.","message":"Bitness mismatch between Python and the VISA library can cause 'No matching architecture' errors. For example, a 64-bit Python interpreter cannot load a 32-bit VISA library.","severity":"gotcha","affected_versions":"All"},{"fix":"Explicitly specify the path to the VISA library when creating the ResourceManager, e.g., `rm = pyvisa.ResourceManager('/path/to/visa.dll')`, or configure it in a `.pyvisarc` file.","message":"The `ResourceManager` might fail to find the VISA library automatically, resulting in an `OSError: Could not find VISA library`.","severity":"gotcha","affected_versions":"All"},{"fix":"Consult your instrument's manual for the correct baud rate, `write_termination`, and `read_termination` characters, and set them on the instrument resource object (e.g., `inst.baud_rate = 9600`, `inst.write_termination = '\\n'`, `inst.read_termination = '\\n'`).","message":"Serial (ASRL) instruments often require specific baud rates and termination characters to be set for correct communication, which are not always the default.","severity":"gotcha","affected_versions":"All"},{"fix":"For delays, use the `query_delay` attribute within `query()` or add delays in your application code. Use `write_termination` and `read_termination` attributes for setting termination characters.","message":"The `Instrument.delay` attribute was removed in PyVISA 1.6. Also, the `term_chars` attribute for setting termination characters is deprecated.","severity":"deprecated","affected_versions":">= 1.6"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}