Python - Error in using import, module --- has in attribute

Asked

Viewed 1,839 times

0

Hello, I’m writing the code for a university project, but when I try to import dependencies to some files, from Error.

I created some package’s that have files that have dependencies, but they don’t find these files.

The entire project is on github, in the same structure as my machine, to help with an eventual solution.

Code repository: ( GITHUB )

File structure:

inserir a descrição da imagem aqui

Start by running the run.py

from agents.agent_defender import Defender

d = Defender()

However the following Error appears.

Traceback (most recent call last):
  File "/home/davi/PycharmProjects/AAMAS/run.py", line 1, in <module>
    from agents.agent_defender import Defender
  File "/home/davi/PycharmProjects/AAMAS/agents/agent_defender.py", line 1, in <module>
    from agents.agent import Agent
  File "/home/davi/PycharmProjects/AAMAS/agents/agent.py", line 1, in <module>
    from .states.state import State
  File "/home/davi/PycharmProjects/AAMAS/agents/states/state.py", line 2, in <module>
    from agents.agent import Agent
ImportError: cannot import name 'Agent'

File -> agent.py (agent_defender is the same Thing)

from .states.state import State

"""
Is basic reference of agent.
"""


class Agent(object):

    state = None
    mail = None  # We use mail, to easily synchronize the agents per turn.
    neighbors = None  # The another agent's of this agent can communicate.

    def __init__(self):
        # check integrity of agent, when is crate

        raise NotImplementedError("subclasses must override __init__ method!\nYou can't instantiate this class!\n")

    def __change_state(self, state):
        # Responsible to change the agent state.

        raise NotImplementedError("subclasses must override change_state method!\n")

    def __valid_state(self):
        # check integrity of agent state, when is crate

        if not self.state:
            raise TypeError("The agent had have a valid state!\n")

        if isinstance(self.state, State):
            raise TypeError("The state of this agent not is instance of ' State class '!\n")

    def __str__(self):
        # Return a basic str info about the agent.

        return self.state.get_string_info()

    def get_info(self):

        return self.state.get_info()

    def recive_msg(self, data):
        # Recive msg, and do what's necessary, whit the data input and put things on mail.

        raise NotImplementedError("subclasses must override recive_msg method!\n")

    def send_msg(self, agent):
        # Send msg, whit data to another agent.

        raise NotImplementedError("subclasses must override send_msg method!\n")

    def do(self):
        # Is the principal method of the agent, because, the interpret the agent variables, and :
        # decide the state, what msg send's, read mail, made actions.
        # The method 'do', pass to state him self, and the state, using the parameters of the agent chose the action.
        # Ex.: self.state.do(self)

        self.state.do(self)

File -> state.py

from interface.model import model
from agents.agent import Agent


class State(object):
    letter = None
    Meaning = None

    def __init__(self):
        raise NotImplementedError("subclasses must override __init__ method!\nYou can't instantiate this class!\n")

    def get_info(self):
        # return info about the state to use on terminal package.

        raise NotImplementedError("subclasses must override get_info method!\n")

    def get_string_info(self):
        # This method implement's the output of the agent state.
        # Using the method model to had a eas pattern of string, receive '1 char' to represent the state

        raise NotImplementedError("subclasses must override string_info method!\n")

    def legend(self):
        # This method return (letter, meaning), info about what letter and your about meaning this state.
        # letter and meaning is string.
        # And if the letter is not be unique, the will raise a Error.

        raise NotImplementedError("subclasses must override legend method!\n")

    def do(self, agent: Agent):
        # this method, receive an agent, interpreter his values and made all necessaries actions.

        raise NotImplementedError("subclasses must override legend method!\n")

If any good soul can show me my mistake,.

1 answer

1

I noticed in the repository that the file __init__.py inside "Events" is empty. It is he who coordinates the loading of modules inside the directory. In your case it should be something like:

from states.state import State

Thence in agente.py you could use:

import states
a = states.State( ... )

Or by importing the class within the program’s own scope:

from states import State
a = State( ... )

To documentation Python has a specific chapter on the definition of modules.

This is one of the problems, the other is with respect to how the modules are organized and being called. For example, the "States" class needs to see "Agent" but does not necessarily need to be inside it as a submodule. So she can be in the same hierarchy.

.
├── agents
│   ├── __init__.py
│   └── agent.py
├── interface
│   ├── __init__.py
│   └── model.py
├── states
│   ├── __init__.py
│   └── state.py
└── run.py

And the calls I focused directly on "run.py":

from agents import Agent, Defender
from interface import Model
from states import State

agent = Defender()
state = State()

print(state.do(agent))

I put the "Agent" and "Defender" classes in the same file agents.py (no need to make a file per class).

class Agent(object):

    def __init__(self):
        self.status = 'agent'

    def __str__(self):
       return "{}".format(self.status)

class Defender(Agent):

    def __init__(self):
        self.status = 'defender'

And the file __init__.py hence makes the charge of both classes:

from agents.agent import Agent, Defender

The other modules are identical to the one shown above.

Ah, and remembering that module loading occurs only within run.py.

  • 1

    Adding out of reply, a while ago I wrote in mine blog something very basic about building modules in Python that might help.

  • I’ll give it a try and feed back.

  • Tested it didn’t work.

  • I supplemented it with some more information, if you want the code skeleton I built.

  • I will implement, and give feedback. Thank you very much for your attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.