Results 1 to 7 of 7

Thread: [Python] Import problem

  1. #1
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    [Python] Import problem

    Hi,

    I have a problem on importing source files in Python. The files look like this:
    Code:
    from mycode.class_a import ClassA
    
    class ClassB:
        # Stuff here
    And the other one
    Code:
    from mycode.class_b import ClassB
    
    class ClassA:
        # Stuff here
    These are imported in another file. I would like to keep one class per file, but I get import error when I have this kind of situation that both classes A and B needs to know each other. What am I missing here?

  2. #2
    Join Date
    Aug 2006
    Location
    Same place as Apple HQ
    Beans
    529
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: [Python] Import problem

    mycode.class_b is importing mycode.class_a which is importing mycode.class_b which is...

    You get the idea. It's a cycle.

    My suggestion? Don't separate classes into different files just because they're different classes. Each file should have everything required related to a certain concept.

  3. #3
    Join Date
    Apr 2006
    Location
    Phoenix, AZ
    Beans
    251
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [Python] Import problem

    Try and remember that the file is a module, you created two seperate modules. If you named them by their class, then you probably had something like this:

    ClassA.ClassA.Method()

    Do what the first reply suggested, move them to the same file (module).
    -Skeeterbug

  4. #4
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [Python] Import problem

    Thank you for your replies. I find it better to write only one class per file, but if there is no way to do this that way, then I just have to join those classes to the same source file. Thanks.

  5. #5
    Join Date
    Aug 2006
    Location
    Same place as Apple HQ
    Beans
    529
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: [Python] Import problem

    Remember, it's not that you can't have one class per file, it's just that if both classes depend on each other, you have to put them in the same file. The most important thing is that Python is not Java. It has its own preferences and best practices, and once you go with the grain, you'll find so much more power in the language.

    Happy coding!

  6. #6
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: [Python] Import problem

    Well, now it works as it should, but I still find it better to organize object-oriented code one class per file. But I can live with this. Thanks to all.

  7. #7
    Join Date
    Aug 2006
    Beans
    366

    Re: [Python] Import problem

    I still find it better to organize object-oriented code one class per file
    If you want to keep 2 files, then you will have to use a third program/file that will import the other 2 and run everything from the 3rd file. This may or may not work, and may even be worse depending on how things are set up. The objective is to have something that works well however it runs.
    Linux Counter entry # 99383 (since 1995), Feisty Xbuntu 64 bit
    Folders! We don't need no stinking folders. "I don't have anything on my machine that needs folding" -- Unknown

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •