2012-08-10 38 views
0

我在lpthw this is the link的鍛鍊50。
並且在很混亂的方式已經下載了lpthw.web框架之後。
我通過編寫一個.py文件繼續練習。LPTHW EX50:找不到模塊utils的

import web 
urls = ('/', 'index') 

app = web.application(urls, globals()) 

class index: 
    def GET(self): 
     greeting = "Hello World" 
     return greeting 

if __name__ == "__main__": 
    app.run() 

,但我得到這個錯誤:

Traceback (most recent call last): 
    File "bin\app.py", line 1, in <module> 
     import web 
    File "c:\Python31\lib\site-packages\web\__init__.py", line 14, in <module> 
     import utils, db, net, wsgi, http, webapi, httpserver, debugerror 
ImportError: No module named utils 

是什麼引起的問題,

我懷疑它是與我至極安裝lpthw.web非常混亂的方式(我有一個很多錯誤,但多次嘗試多種方式。) 預先感謝您!
如果有人需要更多的信息評論,所以我可以編輯。

回答

3

我想你忘了閱讀安裝說明。這種學習指南 python3兼容:從您的系統

Exercise 0: The Setup

You should follow these instructions as exactly as possible. For example, Mac OSX computers already have Python 2, so do not install Python 3 (or any Python).

Make sure you install Python 2 not Python 3.

Warnings For Beginners
A programmer may try to get you to install Python 3 and learn that. You should tell them, "When all of the python code on your computer is Python 3, then I'll try to learn it." That should keep them busy for about 10 years.

刪除python3並安裝python2.7允許本教程當您安裝建議的軟件包工作。

否則,找到另一個使用python3 compatable的libararies中的示例的教程。並非所有內容現在都已更新。出於這個原因,許多人堅持使用python2.7。

+0

你爲什麼要告訴我這個!這是因爲compability問題,因爲我確實有python3而不是python2,但它直到現在可能還沒有造成真正的問題? – user1544624 2012-08-11 08:14:26

+0

我在告訴你,這是你用來教程的網站的直接引用。它不是python3兼容。所以當你開始安裝不使用py3的其他軟件包時,它們就會失敗。許多現有的軟件包還沒有工作。它在人員更新方面進展緩慢。這就是爲什麼許多人留在python2.7上。我已經知道你的路徑中有python3.1。這是Web框架未安裝的原因。刪除它並安裝python 2.7 – jdi 2012-08-11 15:01:14

1

隨着教程的狀態,你可以嘗試使用easy_installpip命令行安裝這將是easy_install lpthw.webpip install lpthw.web

另外請注意,您需要正確引用application
app = application(urls, globals())應該是
app = web.application(urls, globals())
注意^^^部分。

此外,如果你有興趣的蟒蛇微網的框架,我建議bottle.py,它並不需要安裝稱爲bottle.py

更新它只是一個Python文件
我設法重建問題使用python3,所以jdi是正確的,這裏的問題是,python3不與python2向後兼容,一些python2投訴的應用程序將無法正常下python3,反之亦然運行。

在這種情況下python3似乎不包內的支持相對進口,沒有找到這個​​這是很有趣。從賽前

使出:

For the second problem, it is proposed that all import statements be absolute by default (searching sys.path only) with special syntax (leading dots) for accessing package-relative imports.

所以lpthw.web嘗試導入相對軟件包,但它根本不能。 這就是爲什麼我們得到ImportError: No module named utils因爲utils的是包內的相對模塊。
即使它可以加載它,它就會與其他非向後兼容性問題絆倒,只是想包內import utils我們得到

>>> import utils 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "utils.py", line 75 
    except KeyError, k: 
      ^
SyntaxError: invalid syntax 

這是關係到這個PEP http://www.python.org/dev/peps/pep-3110/其中規定不同的語法來的except聲明。

正如你所看到的,它將是一個主要的障礙,試圖將這個軟件包遷移到python3中,以便將它留給開發者,這對於很多很多的軟件包來說都是如此,爲什麼python3與以前不同python2,以及主要是因爲他們想修補語言儘可能...

暫且堅守python2,最終大多數包將被遷移到python3。

+0

我已經使用pip來安裝lpthw.web並且代碼已經有了web.appliction(...),但是我很抱歉只是將它複製到了錯誤的位置。任何想法? – user1544624 2012-08-11 08:11:33

+0

@ user1544624問題是'lpthw.web'不能用於python3,只有python2,我已經更新了答案,我希望它有幫助。 – 2012-08-11 19:43:19