I get an [AttributeError: module 'code' has no attribute 'Main'] error, but the attribute is clearly there. Why?

Question:

I’m trying to link a Discord bot to a text-game I made, but when I attempt to call the Class of the game itself, it tells me,

AttributeError: module 'code' has no attribute 'Main'

Here’s my code:

# bot.py
import code # Import the code for the actual game

main = code.Main() # Begin the game's processes
# code.py
class Main:

    def __init__(self):
        self.otherModule() # This module is used to continue the flow throughout the class

I can’t see what’s wrong with it. When I try to look it up, I’m only told to "just give it a class."

Asked By: Dominic Miller

||

Answers:

Rename code. The module code already exists as a built-in module in Python3.

Python 3.10.4
Type "help", "copyright", "credits" or "license" for more information.
>>> import code
>>> print(code)
<module 'code' from '/usr/lib/python3.10/code.py'>

Source: https://docs.python.org/3/library/code.html#module-code

Answered By: Jeffrey Ram
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.