2017-05-23 130 views
0

我無法在python中導入ssl,它給了我一個錯誤。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 

我使用Linux的卡利,python的版本2.7.12+

我的source.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 

命令李NE:

[email protected]:~/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 

[email protected]:~/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 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 
+0

啓動python,並在其控制檯類型(每條命令在一行中):'import socket'然後'print(socket)'然後'print(dir(套接字))'並粘貼輸出。但是這個錯誤很奇怪:你在哪裏有'import ssl'語句?行號也很奇怪。你有沒有可能修改_socket.py_(來自_Python_庫)?或者你有你自己的文件_socket.py_?如果是這樣,__rename it__,因爲它是一個標準的_Python_模塊名稱。 – CristiFati

+0

不,我沒有修改python庫..我修改了我的問題,現在你的問題的答案就在那裏。 – mfekete101

+2

似乎您仍然在_Python_模塊搜索路徑(_asd.py_也許)旁邊有一個名爲_socket(.py/.pyc/.so)_的文件。修改_asd.py_並添加以下(4)行(在__its beginning__處):'import socket','print(socket)','import sys','print(sys.path)',並粘貼輸出任何例外)。 – CristiFati

回答

3

我相信你的文件被命名爲socket.py,這會產生錯誤(蟒蛇將您的文件的名稱與另一個socket混淆,它是ssl模塊的一部分,它需要impor t,因此具有命名空間類型的錯誤)。

將文件重命名爲其他內容(my_socket.py),問題應該消失。

+0

這解決了我的套接字導入問題,但不是ssl – mfekete101

+0

真的,您當前問題中的錯誤是由命名空間衝突引起的,而不是ssl引起的。請提供有關新錯誤消息(如果有)的更多詳細信息,或者您缺少的預期行爲。 – SimpleBeat

+0

沒有任何變化,問題仍然存在......沒有新的:)錯誤消息,我想我會重新安裝整個操作系統...我嘗試了所有內容,但是Python是邪惡的...... – mfekete101