Imports and Dependencies

Guidance

  • Avoid cyclic imports by moving imports inside functions or methods.
  • Keep module boundaries clear to prevent mutual dependency.

Bad Example

# users.py
from .marketing import send_sms

class User:
    def add_notification(self, message, enable_sms=False):
        if enable_sms:
            send_sms(self.mobile_number, message)
# marketing.py
from .users import User


def query_user_points(users):
    pass

Good Example

# users.py
class User:
    def add_notification(self, message, enable_sms=False):
        if enable_sms:
            from .marketing import send_sms
            send_sms(self.mobile_number, message)

Sources & References