Python - Error in use of import, module - has no attribute

0

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

I created some packages that have files that have dependencies, but they do not find those files.

The project is all in github, in the same structure as my machine, in order to help in an eventual solution.

Repository of code: ( GITHUB )

File structure:

Startrunningrun.py

fromagents.agent_defenderimportDefenderd=Defender()

Butthefollowingerrorappears.

Traceback(mostrecentcalllast):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 error, I will be very grateful.

    
asked by anonymous 05.05.2018 / 02:08

1 answer

0

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

from states.state import State

Hence in agente.py you could use:

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

Or, by importing the class within the scope of the program itself:

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

Python's documentation has a specific chapter on defining modules.

This is one of the problems, the other is with respect to how the modules are organized and called. For example, the "States" class needs to see "Agent" but does not necessarily need to be inside of it as a submodule. So it can stay 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 (there is no need to make one 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 __init__.py file here loads both classes:

from agents.agent import Agent, Defender

The other modules are identical to the one I showed above.

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

    
05.05.2018 / 14:52