0
我正在閱讀編程集體智慧書。在第2章我在做這一步時已經問題在「編程集體智慧」中導入python代碼時出錯
>>>from recommendations import critics
終端顯示此消息
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name critics
整個代碼是
# A dictionary of movie critics and their ratings of a small
# set of movies
critics = {'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,
'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,
'The Night Listener': 3.0},
'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,
'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,
'You, Me and Dupree': 3.5},
'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,
'Superman Returns': 3.5, 'The Night Listener': 4.0},
'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,
'The Night Listener': 4.5, 'Superman Returns': 4.0,
'You, Me and Dupree': 2.5},
'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,
'You, Me and Dupree': 2.0},
'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,
'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},
'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}}
'recommendations.py'是否存儲在你可以訪問它的地方?你有沒有將該文件的路徑追加到'sys.path'?如果你還沒有,那麼Python不會在'recommendations.py'的「right」文件夾中查找。 – blacksite
將'recommendations.py'複製到您啓動'python' REPL的位置。 – MYGz
@not_a_robot非常感謝您的回覆,我會盡全力做到這一點,但終端仍然會顯示相同的信息。 –