Fixing Python pysftp UserWarning: Failed to load HostKeys while connecting to SFTP server

Python pysftp UserWarning: Failed to load HostKeys while connecting to SFTP server is an error which occurs because of a bug in some versions of pysftp.

I’m going to provide a detailed and clear explanation of why this error is happening and how to solve it, I am also going to present other ways to get rid of this problem for good.

Exploring the Python pysftp UserWarning: Failed to load HostKeys while connecting to SFTP server

This is an error which occurs because of a bug in some versions of pysftp.

Please double check the error message bellow so you can avoid mixing between different errors.

                                                                       #
UserWarning: Failed to load HostKeys from C:\Users\...\.ssh\known_hosts.  You will need to explicitly 
load HostKeys (cnopts.hostkeys.load(filename)) or disableHostKey checking (cnopts.hostkeys = None). warnings.warn(wmsg, UserWarning)
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

The Method which solved my problem : Correctly manage trusted host keys.

We have already established the fact that this error is the result of a bug in some versions of pysftp.

Depending on your priority you can opt for a complete solution or a workaround to ignore or supress the warning, or you can do both.

The problem is usually in this line of code

                                                                       #
cnopts.hostkeys = None
                                                                       #

You should start by removing the line above If you want to use pysftp.

Then, create an external file, you can call it myhosts for example. The file should contain the server public key or keys, just like this.

                                                                       #
twitter.com ssh-rsa <thekey>...
                                                                       #

And this is how to include the file in your code in order to manage trusted host keys.

                                                                       #
cnopts = pysftp.CnOpts(knownhosts='myhosts')
with pysftp.Connection(host, username, password, cnopts=cnopts) as sftp:
                                                                       #

If you wish to only supress the warning message. You can do it by installing a module called ‘warningsi.

Import the module and use it in your code like this :

                                                                       #
import warnings
warnings.filterwarnings('ignore')
                                                                       #

By the way, this is going to supress all warning messages.

You can specify the warning you want to ignore , if you want to ignore one warning and not all warnings.

Just like the example bellow, in our case we filter the warning that starts with “Failed to load HostKeys”

                                                                       #
warnings.filterwarnings('ignore','.*Failed to load HostKeys*')
warnings.filterwarnings('ignore','.*SSL ERROR*')
....
                                                                       #

I hope the fix above fixed your problem, good luck with your python projects.

Summing-up : 

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

Thank you for reading my blog post to the end, If you want to learn more about the Python programming language, check out the official Python Documentation : https://docs.python.org/3/