繁体   English   中英

Python SSL导入错误

[英]Python SSL import error

我无法在python中导入ssl,这给了我一个错误。

码:

import ssl

输出(崇高文字3):

Traceback (most recent call last):
  File "asd.py", line 1, in <module>
    import ssl
  File "/usr/lib/python2.7/ssl.py", line 138, in <module>
    from socket import socket, _fileobject, _delegate_methods, error as socket_error
ImportError: cannot import name socket

我正在使用Kali Linux,python的版本是2.7.12+

我的sources.list文件:

# deb cdrom:[Debian GNU/Linux 2016.1 _Kali-rolling_ - Official Snapshot amd64 LIVE/INSTALL Binary 20160830-11:29]/ kali-rolling contrib main non-free

#deb cdrom:[Debian GNU/Linux 2016.1 _Kali-rolling_ - Official Snapshot amd64 LIVE/INSTALL Binary 20160830-11:29]/ kali-rolling contrib main non-free
deb http://http.kali.org/kali kali-rolling main contrib non-free

deb-src http://http.kali.org/kali kali-rolling main contrib non-free
deb http://cz.archive.ubuntu.com/ubuntu yakkety main universe
deb http://http.kali.org/ /kali main contrib non-free
deb http://http.kali.org/ /wheezy main contrib non-free

命令行:

root@PYTH:~/Desktop# python asd.py
Traceback (most recent call last):
  File "asd.py", line 1, in <module>
    import ssl
  File "/usr/lib/python2.7/ssl.py", line 137, in <module>
    from socket import socket, _fileobject, _delegate_methods, error as socket_error
ImportError: cannot import name socket

root@PYTH:~/Desktop# python3 asd.py
Traceback (most recent call last):
  File "asd.py", line 1, in <module>
    import ssl
  File "/usr/lib/python3.5/ssl.py", line 144, in <module>
    from socket import socket, AF_INET, SOCK_STREAM, create_connection
ImportError: cannot import name 'socket'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in <module>
    from urllib.request import urlopen
  File "/usr/lib/python3.5/urllib/request.py", line 88, in <module>
    import http.client
  File "/usr/lib/python3.5/http/client.py", line 739, in <module>
    class HTTPConnection:
  File "/usr/lib/python3.5/http/client.py", line 749, in HTTPConnection
    def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
AttributeError: module 'socket' has no attribute '_GLOBAL_DEFAULT_TIMEOUT'
Original exception was:
Traceback (most recent call last):
  File "asd.py", line 1, in <module>
    import ssl
  File "/usr/lib/python3.5/ssl.py", line 144, in <module>
    from socket import socket, AF_INET, SOCK_STREAM, create_connection
ImportError: cannot import name 'socket'

python命令行:

Python 2.7.12+ (default, Aug  4 2016, 20:04:34) 
[GCC 6.1.1 20160724] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "socket.py", line 1, in <module>
    import ssl
  File "/usr/lib/python2.7/ssl.py", line 137, in <module>
    from socket import socket, _fileobject, _delegate_methods, error as socket_error
ImportError: cannot import name socket
>>> print(socket)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'socket' is not defined
>>> print(dir(socket))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'socket' is not defined

我相信您的文件名为socket.py ,这会产生错误(python将文件名与另一个socket (它需要导入的ssl模块的一部分)混淆,从而导致名称空间类型错误)。

将文件重命名为其他名称( my_socket.py ),问题应该消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM