+ Reply to Thread
Results 1 to 6 of 6

Thread: Python import problem.

  1. #1
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Python import problem.

    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.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python import problem.

    What's the actual code that's generating the error? What's the module directory structure? Do you have circular imports?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: Python import problem.

    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 by Shadow121; 11-09-2009 at 10:42 PM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python import problem.

    Quote Originally Posted by Shadow121 View Post
    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?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    Shadow121's Avatar
    Shadow121 is offline x10 Lieutenant Shadow121 is an unknown quantity at this point
    Join Date
    Jul 2006
    Location
    Centerville
    Posts
    455

    Re: Python import problem.

    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.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python import problem.

    Quote Originally Posted by Shadow121 View Post
    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
    Quote Originally Posted by Shadow121 View Post
    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 by misson; 11-10-2009 at 05:26 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. How to run Python on X10Hosting
    By ah-blabla in forum Tutorials
    Replies: 16
    Last Post: 12-29-2010, 07:01 PM
  2. Another Problem
    By beast474 in forum Free Hosting
    Replies: 1
    Last Post: 08-27-2008, 07:22 PM
  3. Slight Problem
    By rmb1993 in forum Free Hosting
    Replies: 2
    Last Post: 08-14-2008, 06:04 AM
  4. A problem with certificate
    By eon01 in forum Free Hosting
    Replies: 1
    Last Post: 07-31-2008, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers