devdacool devdacool - 1 month ago 12
Python Question

Compiled Python script "No handlers could be found for logger "paramiko.transport"

My problem is very similar to this question already asked:
No handlers could be found for logger paramiko

The difference is that my script will run perfectly fine in the Python interpreter but will throw the mentioned error when compiled with PyInstaller to an exe file. I have tried a couple different logging handlers with no luck, and I'm shooting for a self-containted executable I can run from any Windows system. Here is my script for reference:

from netmiko import ConnectHandler
net_connect = ConnectHandler(device_type='hp_procurve', ip='10.1.2.20', username='myusername', password='mypassword')
output = net_connect.send_command("show run")
print output
net_connect.disconnect()
raw_input("Press enter to exit...")


Thanks for the help!

Answer

Add a console handler (for example) to paramiko.transport:

paramiko_logger = logging.getLogger('paramiko.transport')
if not paramiko_logger.handlers:
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(
        logging.Formatter('%(asctime)s | %(levelname)-8s| PARAMIKO: '
                          '%(lineno)03d@%(module)-10s| %(message)s')
        )
    paramiko_logger.addHandler(console_handler)