12

I have installed the paramiko module. However, when I tried to import that module. I got the following error.

import paramiko
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-42-e77d47aa6e4a> in <module>()
----> 1 import paramiko

C:\Anaconda\lib\site-packages\paramiko\__init__.py in <module>()
     28 
     29 
---> 30 from paramiko.transport import SecurityOptions, Transport
     31 from paramiko.client import SSHClient, MissingHostKeyPolicy, AutoAddPolicy, RejectPolicy, WarningPolicy
     32 from paramiko.auth_handler import AuthHandler

C:\Anaconda\lib\site-packages\paramiko\transport.py in <module>()
     30 
     31 import paramiko
---> 32 from paramiko import util
     33 from paramiko.auth_handler import AuthHandler
     34 from paramiko.ssh_gss import GSSAuth

ImportError: cannot import name util

Does anyone know how to resolve this issue?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Eaton Chow
  • 205
  • 2
  • 4
  • 9
  • Which command did you use to install paramiko? – narendranathjoshi Oct 26 '15 at 09:35
  • Did anyone figure this out? – firebait Aug 02 '16 at 15:30
  • 1
    Just checked on a Linux install, and /usr/lib/python2.7/dist-packages/paramiko/transport.py has the same line (but at 33). This "cannot import name" error often relates to circularly dependent imports, but paramiko doesn't typically have that problem itself (see http://stackoverflow.com/questions/9252543/importerror-cannot-import-name-x for an example). Which version of python? Are you importing other things before your example? – Alex North-Keys Aug 08 '16 at 06:32
  • Was this resolved? I'm having the same issue with python 2.7.6 – Josh C. Dec 20 '16 at 18:00

5 Answers5

2

I have just had the same issue myself (python 2.7.6), and ran into this answer here ImportError: Cannot import name X , which was referred in the question's comments, suggesting it's a circular dependency issue.

After not finding any elegant solution I found myself editing paramiko's source code in site-packages/paramiko/transport.py:

  • Comment / remove the line from paramiko import util
  • Replace every occurrence of util (in this file) to paramiko.util
  • Be careful while replacing: DO NOT replace existing occurrences of paramiko.util

This had fixed the problem for me, leaving me somewhat confused: on the one hand, modifying the import method seems to solve this, but on the other hand Python deals with it in like... 99% of the cases..? Awkward.

Community
  • 1
  • 1
Kludge
  • 2,653
  • 4
  • 20
  • 42
1

We can just edit the import line to be:

from . import util

So we don't have to change every occurrence.

It is strange that after fixing this in paramiko 2.4, I have another import error, which is:

> python
Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/paramiko/__init__.py", line 22, in <module>
    from paramiko.transport import SecurityOptions, Transport
  File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 38, in <module>
    from paramiko.auth_handler import AuthHandler
  File "/usr/lib/python2.7/site-packages/paramiko/auth_handler.py", line 48, in <module>
    from paramiko.ssh_gss import GSSAuth, GSS_EXCEPTIONS
  File "/usr/lib/python2.7/site-packages/paramiko/ssh_gss.py", line 54, in <module>
    GSS_EXCEPTIONS = (gssapi.GSSException,)
AttributeError: 'module' object has no attribute 'GSSException'

It turns out that I have somehow installed cyrus-sasl-gssapi which has the module gssapi but without GSSException. So python is confused. I removed the package and all is fine. If you have python-gssapi, be sure to remove that, too.

It is an issue of paramiko(#1069). But not fixed in paramiko 2.4 for Python 2.7. Have reported it.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
1

Try with reinstalling it. In my case, it works.

pip uninstall paramiko
pip install paramiko --upgrade
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
0

My method is Download python utils from here https://pypi.org/project/utils/#files,and pip install utils-0.9.0-py2.py3-none-any.whl, the problem has been resolved。I think the real cause is without the lib utils。

TCLTAN
  • 1
0

Just to add to Kludge's answer, we will need to replace "util" with paramiko.util" in each of the following files: transport.py, dsskey.py, ber.py, pkey.py, kex_gex.py, kex_group1.py, kex_gss.py, packet.py, primes.py, sftp_client.py, sftp.py, sftp_server.py.

Quoting Kludge's method for better clarity:

  • Comment / remove the line from paramiko import util
  • Replace every occurrence of util (in this file) to paramiko.util
  • Be careful while replacing: DO NOT replace existing occurrences of paramiko.util