0

I have a project (using PySide and sqlalchemy) with the following project structure

bin/
|-- db/     // sqlalchemy mapping to mysql tables
|
|-- model/  // custom TableModels (inherited from QAbstractTableModel)
|
|-- ui/     // ui files created by Qt Designer and the generated python files
|   
|-- view/   // custom TableViews (inherited from QTableView)
|   
|-- widget/ //custom widgets (inherited from QWidget)

My problem started when I created the custom tableViews. Simplifying it, i have a CustomerWidget and an OrderWidget, each of them uses a TableView which displays customer and orders respectively. I made some custom views so that from the customerTableView you can right-click and launch an orderWidget with all of those customers orders. Conversely, from the orderWidget you can right-click it and launch a customerWidget with the customer from that order.

This generated an import problem as customerWidget imports customerView which imports oderWidget which imports orderView which imports customerWidget. (In actuality I have a bunch of widgets that can launch one another).

I was importing using the from ... import syntax. I actually "solved" this by using a regular import.

I was wondering however if there's another, more "elegant", way to solve this, since right now my code is full of stuff like

self._model = bin.model.customerTableModel.CustomerTableModel(args)

and I would really really like to have it as

self._model = CustomerTableModel(args)

Thank you for your help.

Augusto Dias Noronha
  • 854
  • 2
  • 11
  • 20
  • There are a bunch of general questions and answers on this topic. http://stackoverflow.com/questions/894864/circular-dependency-in-python, http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python, http://stackoverflow.com/questions/5748946/pythonic-way-to-resolve-circular-import-statements, http://stackoverflow.com/questions/13354700/python-how-to-refactor-circular-imports – NoDataDumpNoContribution Jan 06 '15 at 13:37

1 Answers1

0
from bin.model import CustomerTableModel
self._model = CustomerTableModel(args)

or

from bin.model import *
self._model = CustomerTableModel(args)

or

import bin.model as bm
CustomerTableModel = bm.CustomerTableModel
self._model = CustomerTableModel(args)
  • That's how I used to do it. In customerWidget I had "from bin.view.customerTableView import CustomerTableView", in customerTableView: "from bin.widget.orderWidget import OrderWidget", in orderWidget: "from bin.view.orderView import OrderView", in orderView: "from bin.widget.customerWidget import CustomerWidget". That way it generates a circular importing error... – Augusto Dias Noronha Dec 23 '14 at 13:26
  • Try to create a __init__.py file inside 'bin' and inside __init__.py file import all stuff. Then import just with 'import bin' in the main file. I guess you will not get an error then. –  Dec 23 '14 at 13:30
  • I see, so then I can "from bin import x"? I can give that a shot – Augusto Dias Noronha Dec 23 '14 at 13:33
  • No and yes. just like "import bin" then inside the bin folder where it is a __init__ .py file which imports all functions. Then you can just do like: "from bin import *" and just do like you wanted. –  Dec 23 '14 at 13:58
  • I see, I'll give that a try. There are many things to change so this will take a while, I'll tell you later if it works – Augusto Dias Noronha Dec 23 '14 at 14:00