2017-01-22 49 views
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}} 
+0

'recommendations.py'是否存储在你可以访问它的地方?你有没有将该文件的路径追加到'sys.path'?如果你还没有,那么Python不会在'recommendations.py'的“right”文件夹中查找。 – blacksite

+0

将'recommendations.py'复制到您启动'python' REPL的位置。 – MYGz

+0

@not_a_robot非常感谢您的回复,我会尽全力做到这一点,但终端仍然会显示相同的信息。 –

回答