1

I am new to python, I was googling and reading SO for this.

I have

pin.py :

from board import Board
class pin(object):
  board_id = Int()
  my_board = store.get(Board, board_id)

  def __init__(self, val): 
    ...

board.py :

from pin import Pin
class Board(object):
   id = Int()
   def __init__(self, val): 
     ...

Board.pins = ReferenceSet(Board.id, Pin.board_id)

As you can see I need to be able to access both Pin and Board from the other class. I saw here to only do import pin and import board. But when I do that and then I do board.Board or pin.Pin for example in my pin.py I'll have my_board = store.get(board.Board, board_id) it gives me this error

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

This wasn't happening when I had the code above, but only didn't have the circular import.

To clarify my question:

How do I do a circular import and call the classes from the files being imported?

Community
  • 1
  • 1
Matilda
  • 1,708
  • 3
  • 25
  • 33
  • my question is how do I do a circular import and make calls to the other class from the current file so store.get(board.Board, board_id) doesn't work for me, given that board is the file to be imported and Board is the class in that file i want to call – Matilda Oct 23 '12 at 20:13

1 Answers1

2

The real answer to your question is "don't use circular imports". Take the stuff that both modules need and put it in a third module, or combine the two modules into one.

To be more specific about what's going on in your case versus the example you linked: you can't safely use circular import references in the top-level module code of the circularly-importing-each-other modules. As the other question you linked to already explains, you must "protect" the circular references by only accessing the module contents inside functions. If you try to use each module directly from the other, as you are doing, it will never work, because neither module can finish running before it tries to use the other one, so neither module will ever get to finish running.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • So it usually make sense not to do that, but in this case, I'm doing a one to many relationship so both classes (models) need to be able to access each other, it kind of doesn't make sense to create another module to just put the common methods in there... any suggestions? – Matilda Oct 23 '12 at 23:48
  • 1
    @Meena: Why are the two classes in separate modules in the first place? – BrenBarn Oct 24 '12 at 00:52
  • Hmm so I'm coming from Ruby background, not sure how python does it. But thinking about it in an ORM activeRecord way. I have a Pin model( table in the db) and a Board model(table in db) so they need to have their own seperate classes that correspond to the tables. so I put them in seperate files... does module refers to Class or file? do you mean I shoudl put two class in the same file? but then if there is a whole bunch of one-to-many and many-to-many relationships we end up with a large file full of classes. Is that how it's been done in python? – Matilda Oct 24 '12 at 01:40
  • 1
    @Meena: A module is a file. Unlike in some other languages, in Python there is no real reason to put each class in its own file. Note that another possibility is to wrap the code that refers to the other class in a method. You can then successfully define the class, and *later* call the method to set up the relationship. The only reason your code causes problems is that the circular references are directly in the class body and not in functions/methods. – BrenBarn Oct 24 '12 at 02:35