question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you deal with circular imports ?

See original GitHub issue

For example I have models like this :

# modules/patients/models.py
class Patient(db.Model, OwnerMixin):	
	__owner_backref_name__ = "patients"

	id = db.Column(db.Integer, primary_key=True)

	phone = db.Column(db.String)
	email = db.Column(db.String)
	first_name = db.Column(db.String)
	last_name = db.Column(db.String)
	birthday = db.Column(db.DateTime)
	zip_code = db.Column(db.String)
	address = db.Column(db.String)

        # I would like to do this, but there's circular import....
       @aggregated('claims', db.Column(db.Integer))
	def unpaid_amount(self):
		return db.func.count('1')

# modules/accounting/models.py
class Claim(db.Model, OwnerMixin):
	__owner_backref_name__ = 'claims'

	id = db.Column(db.Integer, primary_key=True)

	date = db.Column(db.DateTime, nullable=False)
	label = db.Column(db.String, nullable=False)
	amount = db.Column(db.Float, nullable=False)
	ref = db.Column(db.String)	

	patient_id = db.Column(db.Integer, db.ForeignKey('patient.id'), nullable=False)
	patient = db.relationship(
				Patient,
				backref=db.backref('claims', cascade="delete, delete-orphan")
	)

Patient is in dedicated module “patient” and claim model is in ‘accounting’ modules with other models related to accounting. The problem is I can’t import claim model and I can’t access to claim model in patient model in order to, for example, make the sum of claims amount. I think this is some design issue, but I want to be sure if there’s any workaround before rewriting…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
askzcommented, Jun 30, 2017

Yes this is what I’ve been up these last minutes. Many thanks. I resolved it with this solution plus :

	@aggregated('claims', db.Column(db.Integer))
	def unpaid_sum(self):
		return db.func.sum(Claim.amount)
1reaction
khoroletscommented, Jun 30, 2017

@askz And what about @frol’s advice about providing related model as string without importing it? Didn’t it help?

patient = db.relationship(
    'Patient',
    backref=db.backref('claims', cascade="delete, delete-orphan")
)

@frol should it work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Circular Imports
Circular imports are a specific case of circular references. Generally, they can be resolved with better code design. However, sometimes, the ...
Read more >
Pythonic way to resolve circular import statements?
Is there a Pythonic way to deal with circular dependencies like this and still keep import statements at the top of the file?...
Read more >
Avoiding Circular Imports in Python | by André Menck
The notable thing here is that in order to resolve this circular import, we actually had to move a class definition into another...
Read more >
Python Circular Import Problem and Solutions
How to fix Python Circular Import? · Importing The Module Itself · Rename Your Working file · Avoid Circular Import Calls.
Read more >
Python Type Hints - How to Fix Circular Imports - Adam Johnson
Circular imports are always annoying when they arise in Python, and type hints make them more common. Thankfully, there's a trick to add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found