【问题标题】:Python3 - "NameError: ... " when importing from ExpertaPython3 - 从 Experta 导入时出现“NameError: ...”
【发布时间】:2020-05-23 02:46:23
【问题描述】:

你好,
我使用 Python 3.8.3 在 Conda 环境中安装了 Experta,我的所有程序都可以正常工作,但是当我尝试运行此代码导入 Experta 库时,我遇到以下错误:“NameError: name 'Fact' is not defined”。

from random import choice
from experta import *


class Light(Fact):
    """Info about the traffic light."""
    pass


class RobotCrossStreet(KnowledgeEngine):
    @Rule(Light(color='green'))
    def green_light(self):
        print("Walk")

    @Rule(Light(color='red'))
    def red_light(self):
        print("Don't walk")

    @Rule(AS.light << Light(color=L('yellow') | L('blinking-yellow')))
    def cautious(self, light):
        print("Be cautious because light is", light["color"])


engine = RobotCrossStreet()
engine.reset()
engine.declare(Light(color=choice(['green', 'yellow', 'blinking-yellow', 'red'])))
engine.run()

错误:

Traceback (most recent call last):
File "experta.py", line 2, in <module>
  from experta import *
File "/home/karuro/Documents/Magierin/UPN/IntelligentSystems/Experta/experta.py", line 5, in <module>
  class Light(Fact): 
NameError: name 'Fact' is not defined

谁能帮我解决这个问题?

【问题讨论】:

  • 你真的不应该这样做 from experta import * 加星标的导入是不好的。我不熟悉这个库,我只能推测你希望它包含Fact,但显然它没有。为什么你认为应该这样做?
  • 等等,你把文件命名为experta.py了吗?不要那样做,那会影响您安装的库。请始终将完整的错误消息发布为格式化文本 I。问题本身*请勿发布图片/屏幕截图或链接。
  • @juanpa.arrivillaga 它通过使用另一个名称复制文件来工作。感谢您的帮助和建议,我编辑问题以文本格式显示错误。

标签: python import conda nameerror python-3.8


【解决方案1】:

检查是否安装了experta,如果没有尝试使用--user(root)安装

pip install experta --user

【讨论】:

  • 感谢您的回答。已经安装好了,问题是文件名。
  • @Karuro 请点赞或勾选我的回答,让其他人觉得它有用
  • 我做到了,但我没有足够的声誉让它反映在页面上。
【解决方案2】:

使用名称“experta.py”执行的问题。

解决方案:
用不同的名称克隆文件并重新运行程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多