导入paramiko库时报 UnicodeDecodeError 错误的解决方案

背景:

远程控制服务器实现日志的抓取、文件的上传等。可能很多同学会使用到paramiko库。

环境:

如果实在Windows环境下需安装VCForpython27.msi。然后需安装paramiko库。(安装先后顺序可能不能颠倒,同学们可以试一下)

切入正题吧。

问题:

导入paramiko库时报 UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 3: ordinal not in range(128) 错误

详细错误如下:

C:\Python27\python.exe F:/download.py
Traceback (most recent call last):
  File "F:/download.py", line 5, in
    import paramiko
  File "C:\Python27\lib\site-packages\paramiko\__init__.py", line 31, in
    from paramiko.transport import SecurityOptions, Transport
  File "C:\Python27\lib\site-packages\paramiko\transport.py", line 56, in
    from paramiko.dsskey import DSSKey
  File "C:\Python27\lib\site-packages\paramiko\dsskey.py", line 27, in
    from cryptography.hazmat.primitives.asymmetric.utils import (
  File "C:\Python27\lib\site-packages\cryptography\hazmat\primitives\asymmetric\utils.py", line 9, in
    from asn1crypto.algos import DSASignature
  File "C:\Python27\lib\site-packages\asn1crypto\algos.py", line 23, in
    from ._int import fill_width
  File "C:\Python27\lib\site-packages\asn1crypto\_int.py", line 56, in
    from ._perf._big_num_ctypes import libcrypto
  File "C:\Python27\lib\site-packages\asn1crypto\_perf\_big_num_ctypes.py", line 31, in
    libcrypto_path = find_library('crypto')
  File "C:\Python27\lib\ctypes\util.py", line 53, in find_library
    fname = os.path.join(directory, name)
  File "C:\Python27\lib\ntpath.py", line 85, in join
    result_path = result_path + p_path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 3: ordinal not in range(128)



Process finished with exit code 1

解决问题历程:

1、该代码以前是调试通过了的,后来有重新装了PYTHON环境。所以怀疑环境问题。故重新安装了VCForpython27和paramiko库

                                                                         ----问题没有得到解决

2、看错误日志最后出问题的地方是:result_path = result_path + p_path。且提示解码出了问题。故可能是中文问题。在代码中加了

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
然并卵,还是报同样的错误

3、因为Windows下的编码是gbk,故可能在导入库运行路径是不能解码。方案1:修改Lib\site.py,加上

# import sys
# reload(sys)
# sys.setdefaultencoding('gbk')

方案2:在paramiko库前面加上

# import sys
# reload(sys)
# sys.setdefaultencoding('gbk')

大功告成!!!!


报错截图:



你可能感兴趣的:(自动化实现远程控制)