Python import problem.

Shadow121

Member
Messages
901
Reaction score
0
Points
16
I'm having a problem importing classes into my python. It works partially such as I can load the DB class/module and the other class/module but in the other I can not load the IRC module back into it. I want top know how I would be able to do so D:

This is what I currently get

AttributeError: 'module' object has no attribute 'irc'

But, the irc module has a class called irc which I loaded in my main python file all:

ircHandle = irc.irc() which works. But, in the other module it will not work.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
What's the actual code that's generating the error? What's the module directory structure? Do you have circular imports?
 

Shadow121

Member
Messages
901
Reaction score
0
Points
16
I officially hate the random question. Right now, the sky is Dark. Not blue. :(

Code:
import random, db, irc

class other:
    ircHandle = irc.irc()
    def __init__(self):
        self.dbHandle = db.db()
    def newLine(self):
        self.ircHandle.ircTest()
        print ""
    def makeNewNick(self):
        myNewNick = "guest" + str(random.randint(1,1024))
        return myNewNick
That's the code. I redid the import thing according to the link you gave me and it still doesn't work.

Then here is the error

Code:
Traceback (most recent call last):
  File "ngtwlkr.py", line 1, in <module>
    from Resources import *
  File "L:\Documents\Site\Bot\Resources\__init__.py", line 2, in <module>
    from Resources import irc
  File "L:\Documents\Site\Bot\Resources\irc.py", line 1, in <module>
    import socket, sys, string, db, other
  File "L:\Documents\Site\Bot\Resources\other.py", line 3, in <module>
    class other:
  File "L:\Documents\Site\Bot\Resources\other.py", line 4, in other
    ircHandle = irc.irc()
AttributeError: 'module' object has no attribute 'irc'

Edit: Managed to fix it and this is my code;
Code:
import random, db

class other:
    def __init__(self):
        self.dbHandle = db.db()
    def newLine(self):
        import irc
        self.ircHandle = irc.irc()
        print ""
    def makeNewNick(self):
        myNewNick = "guest" + str(random.randint(1,1024))
        return myNewNick

But, the thing is what if I add more things that need the irc? Would I have to import it time after time? O-o
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
But, the thing is what if I add more things that need the irc? Would I have to import it time after time? O-o
Note that you don't need to move the "import" to the function (since you're using the "import module" form, not "from module import names".

Class names should be capitalized according to the CapWords convention.

Initializing the ircHandle instance variable should be done in the __init__ method:
Code:
import random, db, irc

class Other:
    def __init__(self):
        self.dbHandle = db.Db()
        self.ircHandle = irc.Irc()
    
    def newLine(self):
        print ""
    
    def makeNewNick(self):
        myNewNick = "guest" + str(random.randint(1,1024))
        return myNewNick

Does ircHandle need to be shared between every instance of other?
 

Shadow121

Member
Messages
901
Reaction score
0
Points
16
Yes. it is needed through out the script. But, if I put it at the top, it still says It can't load it due to the fact of the circular import.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Yes. it is needed through out the script.
Not do all the methods of Other need ircHandle; does every instance of Other need the same ircHandle? That is:
Code:
one=Other()
another=Other()
# does the following need to be True?
one.ircHandle is another.ircHandle

But, if I put it at the top, it still says It can't load it due to the fact of the circular import.
"import irc" at the beginning of other.py should be fine, you just can't create an instance of irc.Irc until processing of the Irc class in irc.py has been finished.
 
Last edited:
Top