首页 > 解决方案 > Python3 - 从 Experta 导入时出现“NameError: ...”

问题描述

您好,
我使用 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

谁能帮我解决这个问题?

标签: pythonimportcondanameerrorpython-3.8

解决方案


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

pip install experta --user

推荐阅读