title: "No hostkey for ... found" in pysftp code even though cnopts.hostkeys is set to None
body: <p><strong>Background</strong></p>
  
  <p>To SFTP across to another server I use the following command in the UNIX command line: </p>
  
  <pre class="lang-none prettyprint-override"><code>sftp -i /some_dir/another_dir/key -oPort=12345 user@12.123.456.789
  </code></pre>
  
  <p><strong>What I want to achieve</strong></p>
  
  <p>I want to convert this to command to be used with <a href="https://www.pythonforbeginners.com/modules-in-python/python-secure-ftp-module" rel="nofollow noreferrer"><code>PySFTP</code></a>.</p>
  
  <p><strong>What I have tried</strong></p>
  
  <pre><code>import pysftp
  cnopts = pysftp.CnOpts()
  cnopts.hostkeys = None
  
  # source: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html
  srv = pysftp.Connection("user@12.123.456.789", port="12345",  
                           private_key_pass="/some_dir/another_dir/key")
  </code></pre>
  
  <p><strong>Errors encountered</strong></p>
  
  <pre class="lang-none prettyprint-override"><code>File "./aws_sql_dumper.py", line 14, in &lt;module&gt;
      srv = pysftp.Connection("user@12.123.456.789", port="12345",  private_key_pass="/some_dir/another_dir/key")
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 132, in __init__
      self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 71, in get_hostkey
      raise SSHException("No hostkey for host %s found." % host)
  paramiko.ssh_exception.SSHException: No hostkey for host user@12.123.456.789 found.
  Exception ignored in: &lt;bound method Connection.__del__ of &lt;pysftp.Connection object at 0x7f6067c7ea20&gt;&gt;
  Traceback (most recent call last):
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 1013, in __del__
      self.close()
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 784, in close
      if self._sftp_live:
  AttributeError: 'Connection' object has no attribute '_sftp_live'
  </code></pre>
  
  <p><strong>Research done</strong></p>
  
  <p>I've read up on the following:</p>
  
  <ul>
  <li><a href="https://bitbucket.org/dundeemt/pysftp/issues/97/pysftp-029-breaks-builds" rel="nofollow noreferrer">pysftp 0.2.9 breaks builds</a></li>
  <li><a href="https://pysftp.readthedocs.io/en/release_0.2.9/" rel="nofollow noreferrer">Welcome to pysftp’s documentation!</a></li>
  <li><a href="https://pythonprogramming.net/community/281/Stuck%20in%20a%20pysftp%20Dilemma%20-%20Needing%20Help/" rel="nofollow noreferrer">Stuck in a pysftp Dilemma - Needing Help</a></li>
  <li><a href="https://stackoverflow.com/questions/38939454/verify-host-key-with-pysftp">S/O - Verify host key with pysftp</a></li>
  </ul>
  
  <p><strong>Question</strong></p>
  
  <p>I think I am doing something fundamental wrong. How can I adopt the SFTP command used in UNIX command line to be accepted/interpretted by <code>pysftp</code>? </p>
  
  <hr>
  
  <p><strong>Changes</strong></p>
  
  <p>I changed a few things around</p>
  
  <pre><code>import pysftp
  cnopts = pysftp.CnOpts()
  cnopts.hostkeys.load('/home/some_dir/.ssh/known_hosts')
  
  # source: https://pysftp.readthedocs.io/en/release_0.2.7/pysftp.html
  srv = pysftp.Connection("user@12.123.456.789", port="12345",  
                           private_key="/some_dir/another_dir/key", cnopts=cnopts)
  </code></pre>
  
  <p><strong>Output after change</strong></p>
  
  <pre><code>Traceback (most recent call last):
    File "./aws_sql_dumper.py", line 17, in &lt;module&gt;
      srv = pysftp.Connection("user@12.123.456.789", port="12345",  private_key="/some_dir/another_dir/key", cnopts=cnopts)
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 132, in __init__
      self._tconnect['hostkey'] = self._cnopts.get_hostkey(host)
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 71, in get_hostkey
      raise SSHException("No hostkey for host %s found." % host)
  paramiko.ssh_exception.SSHException: No hostkey for host user@12.123.456.789" found.
  Exception ignored in: &lt;bound method Connection.__del__ of &lt;pysftp.Connection object at 0x7f8120dc6438&gt;&gt;
  Traceback (most recent call last):
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 1013, in __del__
      self.close()
    File "/usr/local/lib/python3.4/dist-packages/pysftp/__init__.py", line 784, in close
      if self._sftp_live:
  AttributeError: 'Connection' object has no attribute '_sftp_live
  </code></pre>