pythonpyvisa

Stop PyVISA from writing to the console in Spyder


I have some code which utilizes the PyVISA package to control a detector through USB. The code runs perfectly fine, but PyVISA dumps a bunch of debug information in the console which is making it difficult for me to print to the console to debug myself. Code and console printout below.

My code:

def pmt_onoff_test(gain_level = 0, offset_voltage = -0.009, lpfreq = 80000000):
    gain_voltage = PMT1_gain_levels['voltage'].iloc[int(gain_level)

    rm = pyvisa.ResourceManager()
    pmt1 = rm.open_resource(pmt1_address)
    
    pmt1.write("INST:SEL GAIN")
    pmt1.write("SOUR:VOLT %f" % gain_voltage)

    pmt1.write("INST:SEL OFFSET") 
    pmt1.write("SOUR:VOLT %f" % offset_voltage)

    pmt1.write("SENS:FILT:LPAS:FREQ %f" % lpfreq)
    
    pmt1.write("SENS:FUNC:ON %s" % pmt2101_model)
    time.sleep(5)
    pmt1.write("SENS:FUNC:OFF %s" % pmt2101_model)
    
    rm.close()

Console output:

C:\ProgramData\anaconda3\envs\multiphoton\Lib\site-packages\nidaqmx\task\_task.py:102: DaqResourceWarning: Task of name "_unnamedTask<2C>" was not explicitly closed before it was destructed. Resources on the task device may still be reserved.
  warnings.warn(
C:\ProgramData\anaconda3\envs\multiphoton\Lib\site-packages\nidaqmx\task\_task.py:102: DaqResourceWarning: Task of name "_unnamedTask<2D>" was not explicitly closed before it was destructed. Resources on the task device may still be reserved.
  warnings.warn(
C:\ProgramData\anaconda3\envs\multiphoton\Lib\site-packages\nidaqmx\task\_task.py:102: DaqResourceWarning: Task of name "_unnamedTask<2E>" was not explicitly closed before it was destructed. Resources on the task device may still be reserved.
  warnings.warn(
DEBUG:pyvisa:No visa library specified, trying to find alternatives.
DEBUG:pyvisa:Environment variable PYVISA_LIBRARY is unset.
DEBUG:pyvisa:No user defined configuration
DEBUG:pyvisa:Automatically found library files: [None, 'C:\\Windows\\system32\\visa32.dll', 'C:\\Windows\\system32\\visa32.dll', 'C:\\Windows\\system32\\visa64.dll', 'C:\\Windows\\system32\\visa64.dll']
DEBUG:pyvisa:No user defined library files
DEBUG:pyvisa:The IVI implementation available
DEBUG:pyvisa:No user defined configuration
DEBUG:pyvisa:Automatically found library files: [None, 'C:\\Windows\\system32\\visa32.dll', 'C:\\Windows\\system32\\visa32.dll', 'C:\\Windows\\system32\\visa64.dll', 'C:\\Windows\\system32\\visa64.dll']
DEBUG:pyvisa:No user defined library files
DEBUG:pyvisa:Reusing ResourceManager with session 29
DEBUG:pyvisa:viParseRsrcEx(29, 'USB0::0x1313::0x2F00::00AH3403::0::INSTR', 'c_ushort(7)', 'c_ushort(0)', <ctypes.c_char_Array_256 object at 0x000001C6DBE57F50>, <ctypes.c_char_Array_256 object at 0x000001C6DBE542D0>, <ctypes.c_char_Array_256 object at 0x000001C6DBE55650>) -> 0
DEBUG:pyvisa:USB0::0x1313::0x2F00::00AH3403::0::INSTR - opening ...
DEBUG:pyvisa:viOpen(29, 'USB0::0x1313::0x2F00::00AH3403::0::INSTR', <AccessModes.no_lock: 0>, 0, '<ViObject object at 0x000001C6DBE57F50>') -> 0
DEBUG:pyvisa:USB0::0x1313::0x2F00::00AH3403::0::INSTR - is open with session 31
DEBUG:pyvisa:viWrite(31, b'INST:SEL GAIN\r\n', 15, 'c_ulong(15)') -> 0
DEBUG:pyvisa:viWrite(31, b'SOUR:VOLT 0.529000\r\n', 20, 'c_ulong(20)') -> 0
DEBUG:pyvisa:viWrite(31, b'INST:SEL OFFSET\r\n', 17, 'c_ulong(17)') -> 0
DEBUG:pyvisa:viWrite(31, b'SOUR:VOLT -0.009000\r\n', 21, 'c_ulong(21)') -> 0
DEBUG:pyvisa:viWrite(31, b'SENS:FILT:LPAS:FREQ 80000000.000000\r\n', 37, 'c_ulong(37)') -> 0
DEBUG:pyvisa:viWrite(31, b'SENS:FUNC:ON H10770PA-40\r\n', 26, 'c_ulong(26)') -> 0
DEBUG:pyvisa:viWrite(31, b'SENS:FUNC:OFF H10770PA-40\r\n', 27, 'c_ulong(27)') -> 0
DEBUG:pyvisa:Closing ResourceManager (session: 29)
DEBUG:pyvisa:USB0::0x1313::0x2F00::00AH3403::0::INSTR - closing
DEBUG:pyvisa:viDisableEvent(31, <EventType.all_enabled: 1073709055>, <EventMechanism.all: 65535>) -> 0
DEBUG:pyvisa:viDiscardEvents(31, <EventType.all_enabled: 1073709055>, <EventMechanism.all: 65535>) -> 1073676292
DEBUG:pyvisa:viClose(31,) -> 0
DEBUG:pyvisa:USB0::0x1313::0x2F00::00AH3403::0::INSTR - is closed
DEBUG:pyvisa:viClose(29,) -> 0

I tried searching the PyVISA API documentation for some option to turn off debug information but couldn't find anything. Does anyone have suggestions for suppressing this output in the console?


Solution

  • Problem makes standard module logging and you would have to search help in logging

    I see pyvisa messages only when I create logger with level DEBUG

    Minimal working example

    import pyvisa
    import logging
    
    logging.basicConfig(level=logging.DEBUG)
    
    logging.debug('testing DEBUG')   # I see this message
    logging.info('testing INFO')     # I see this message
    
    rm = pyvisa.ResourceManager()    # I see PyVISA messages
    print(rm.list_resources())       # I see PyVISA message
    

    I can deactivate all messages using logging.disable()

    import pyvisa
    import logging
    
    logging.basicConfig(level=logging.DEBUG)
    
    logging.debug('testing DEBUG')   # I see this message
    logging.info('testing INFO')     # I see this message
    
    logging.disable()
    
    logging.debug('testing DEBUG')   # I don't see this message
    logging.info('testing INFO')     # I don't see this message
    
    rm = pyvisa.ResourceManager()    # I don't see PyVISA messages
    print(rm.list_resources())       # I don't see PyVISA message
    

    I can also change configuration only for logger with name pyvisa
    and if I use level INFO (or other) then it skips PyVISA messages,
    but I still can display own messages

    import pyvisa
    import logging
    
    logging.basicConfig(level=logging.DEBUG)
    
    # get logger by its name
    log = logging.getLogger('pyvisa')
    log.level = logging.INFO
    
    logging.debug('testing DEBUG')   # I see this message
    logging.info('testing INFO')     # I see this message
    
    rm = pyvisa.ResourceManager()    # I don't see PyVISA messages
    print(rm.list_resources())       # I don't see PyVISA message