简体   繁体   中英

Creating An Object From a Different File in Python

I am trying a project which involves multiple files. . This object is a composition of two objects. The files are on Py Charm 2020.3. I have tried adding a init as shown in this question ( Create Python object from different.py file ) The error message I receive is that there is no module. The whole error message is below.

Traceback (most recent call last): File "C:/Users/Aku/ImportExports/Message/TellAndReciveMessage.py", line 2, in from Tell import TellMessage ModuleNotFoundError: No module named 'Tell'

The Tell Message File:


class TellMessage:
    def __init__(self, msg:str) -> None:
        self.msg = msg

    def show(self) -> None:
        print(self.msg)


def main() -> None:
    tell_message = TellMessage("hello world")
    tell_message.show()

if __name__ == "__main__":
    main()

#Tell and Receive Message File 


from Message import TellMessage

class TellAndReciveMessage:
    def __init__(self, tell:TellMessage, recived:str):
        self.tell = tell
        self.recived = ""

    def get_recived(self) -> str:
        return self.recived

    def show(self) -> None:
        self.tell.show()
        self.recived = input(">>")

def main() -> None:
    tell = TellMessage("hello world")
    both = TellAndReciveMessage(tell, "")
    tell.show()
    print(tell.get_recived())

if __name__ == "__main__":
    main()


``

''''

"""" Init Module of package """

""" Tell Message File """

class TellMessage: def init (self, msg:str) -> None: self.msg = msg

def show(self) -> None:
    """
    Command line output
    """
    print(self.msg)

def make(msg:str) -> TellMessage: """:param msg: msg to recive Output: A new Tell and Reicive Class Note: This function can be called by the other class """ return TellMessage(msg)

def main() -> None: """ Input: None Output: None Note: Outputs result on command line """ tell_message = TellMessage("hello world") tell_message.show()

if name == " main ": main()

""" Tell and Recive Message Module """

from Message import TellMessage

class TellAndReciveMessage: def init (self, tell:TellMessage, recived:str): self.tell = tell self.recived = ""

def get_recived(self) -> str:
    """
    Output: recived string (idenpotent)
    """
    return self.recived

def show(self) -> None:
    """
    Shows output and gets input
    Output: None
    """
    if not self.tell:
        self.recived = input(">>")
        return
    self.tell.show()
    self.recived = input(">>")

def main() -> None: """ Input: None Output: None Note: Runs code and output on command line

"""

#Here is the line that fixes it (can call function)
tell = TellMessage.make("hello world")
both = TellAndReciveMessage(None, "")
both.show()
print(both.get_recived())

if name == " main ": main()

''''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM