Fehler beim Importieren von Agentenmodul aus dem übergeordneten VerzeichnisPython

Python-Programme
Anonymous
 Fehler beim Importieren von Agentenmodul aus dem übergeordneten Verzeichnis

Post by Anonymous »

Ich versuche, in Python zu beginnen und an einem einfachen Programm zu arbeiten, das die AIMA -Bibliothek verwendet. Ich habe die AIMA -Bibliothek importiert und alle Dateien sind in einem übergeordneten Verzeichnis enthalten. Ich habe ein Unterverzeichnis erstellt und in diesem Unterverzeichnis habe ich eine einzige .py -Datei. Dies sollte ermöglichen, dass die .py -Datei auf einem Computer ausgeführt werden kann, auf dem die AIMA -Bibliothek im übergeordneten Verzeichnis vorliegt. < /P>

Code: Select all

# Get the parent directory of the current file and print the path for          verification - alternative method to above
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
print(f"Parent dir is {parent_dir}")

# Add the parent directory to sys.path and print the path for verification
sys.path.insert(0, parent_dir)
print(f"Sys path is {sys.path}")

import agents

# Verify that the agents module has been imported successfully
print ('Successfully imported agents from AIMA library')
print("agents module:", agents)
print("Has TrivialVacuumEnvironment:", hasattr(agents, "TrivialVacuumEnvironment"))

class BlindDog(Agent):
def eat(self, thing):
print("Dog: Ate food at {}.".format(self.location))

def drink(self, thing):
print("Dog: Drank water at {}.".format( self.location))

dog = BlindDog()`
< /code>
Ausführen dieses Codes Ich hätte erwartet, dass das Agentenmodul korrekt eingerichtet wurde, damit der Blinddog -Agent definiert werden kann. Ich erhalte jedoch die Ausgabe unten, in der die Pfade korrekt definiert werden, aber ich erhalte immer noch einen Fehler. Ich denke, ich mache etwas wirklich grundlegend falsch, kann es aber an diesem Punkt nicht sehen.Parent dir is /Users/brianbarry/Library/CloudStorage/OneDrive-Personal/Documents/Education/Artificial Intelligence PGradDip/Knowledge Representation/Labs & Assignments/AIMA Folder/aima-python
Sys path is ['/Users/brianbarry/Library/CloudStorage/OneDrive-Personal/Documents/Education/Artificial Intelligence PGradDip/Knowledge Representation/Labs & Assignments/AIMA Folder/aima-python', '/Users/brianbarry/Library/CloudStorage/OneDrive-Personal/Documents/Education/Artificial Intelligence PGradDip/Knowledge Representation/Labs & Assignments/AIMA Folder/aima-python/Assignment_1', '/opt/anaconda3/envs/24COMP9016/lib/python39.zip', '/opt/anaconda3/envs/24COMP9016/lib/python3.9', '/opt/anaconda3/envs/24COMP9016/lib/python3.9/lib-dynload', '/opt/anaconda3/envs/24COMP9016/lib/python3.9/site-packages']
Successfully imported agents from AIMA library
agents module: 
Has TrivialVacuumEnvironment: True
Traceback (most recent call last):
File "/Users/brianbarry/Library/CloudStorage/OneDrive-Personal/Documents/Education/Artificial Intelligence PGradDip/Knowledge Representation/Labs & Assignments/AIMA Folder/aima-python/Assignment_1/A1.py", line 38, in 
class BlindDog(Agent):
NameError: name 'Agent' is not defined

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post