【发布时间】:2017-10-23 11:43:43
【问题描述】:
我无法在 python 中导入 ssl,它给了我一个错误。
代码:
import ssl
输出(Sublime Text 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
【问题讨论】:
-
启动 python 并在其控制台类型中(每个命令在一行):
import socket然后print(socket)然后print(dir(socket)),并粘贴输出。但是错误很奇怪:import ssl语句在哪里?行号也很奇怪。你有没有修改过 socket.py (来自 Python 库)?或者您有自己的文件,名为 socket.py?如果是这样,请重命名,因为它是标准的 Python 模块名称。 -
不,我没有修改python库..我修改了我的问题,你的问题的答案现在就在那里。
-
好像你在 Python 模块搜索路径中还有一个名为 socket(.py/.pyc/.so) 的文件(在 i>asd.py 也许)。修改 asd.py 并添加以下 (4) 行(在 其开头):
import socket,print(socket),import sys,print(sys.path),然后粘贴输出(没有任何例外)。 -
现在它修复了!谢谢!另外,我不得不重新启动电脑..(我不知道为什么)