2016-07-20 29 views
4

我試圖打開了一個網址爲我的項目,這裏是我的代碼:導入錯誤:無法導入名稱的urlopen

from urllib2 import urlopen 
page = urlopen("https://docs.python.org/3/howto/urllib2.html") 
contents = page.read() 

這只是一個演示一個簡單的代碼但是,當我運行代碼,我得到了以下錯誤「導入錯誤:無法導入名稱的urlopen」

我試着輸入「PIP安裝的urllib2」進入CMD並得到了以下錯誤,以及「無法找到滿足需求的urllib2版本...找不到與urllib2匹配的分佈「

我如何解決這個錯誤,因爲我使用python 2.7.12而不是python3

+0

這對我的作品。你可以運行導入urllib2;打印urllib2 .__ version__ – sahutchi

+0

其實,另一個問題 - 你使用python 2或3嗎? – sahutchi

+0

urllib2是在python標準庫中,所以你不應該點擊安裝它。 – dmlicht

回答

1

這個問題的答案分爲兩部分。該解決方案有所不同,如果您使用蟒2或Python 3.

在Python 3的urllib2不再使用基礎上的。嘗試使用urllib.request。

在Python 2,你可能只是有urllib2的一個壞的安裝或舊版本。嘗試pip安裝urllib2。

+0

進一步的研究是,這裏回答了這裏:http://stackoverflow.com/questions/2792650/python3-error-import-error-no-module-name-urllib – sahutchi

+0

我修改了我的問題! – plzhelp

9

我打算接受一個有教養的猜測,並假設你正在使用python3。在python3中,urllib2已被拆分爲urllib.requesturllib.errorSee note at the top of the urllib2 page。您正在查找的功能包含在urllib.request中。請嘗試以下操作:

from urllib.request import urlopen 
page = urlopen("https://docs.python.org/3/howto/urllib2.html") 
contents = page.read() 
+0

我編輯了我的問題! – plzhelp

相關問題