【问题标题】:wrong when import python code in "programming collective intelligence"在“编程集体智慧”中导入 python 代码时出错
【发布时间】:2017-01-23 02:09:45
【问题描述】:

我正在阅读编程集体智慧书。在第 2 章中,我在执行此步骤时遇到问题

>>>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
  • recommendations.py复制到您开始python REPL的位置。
  • @not_a_robot 谢谢你的重播,我做了所有这些步骤,但终端仍然显示相同的消息。

标签: python


【解决方案1】:

我没有读过这本书,但看起来您需要下载“推荐”模块才能完成示例。这是https://github.com/arthur-e/Programming-Collective-Intelligence/blob/master/chapter2/recommendations.py 附: 你需要把这个文件放在你的工作目录中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2023-04-03
    相关资源
    最近更新 更多