zhanymkanov / fastapi-best-practices

FastAPI Best Practices and Conventions we used at our startup
8.84k stars 672 forks source link

Circular import #26

Closed jhoanmartinez closed 1 year ago

jhoanmartinez commented 1 year ago

How you guys manage to avoid cirular import errors? I am following this structure but circular import just came out.

zhanymkanov commented 1 year ago

Usually, I don't get circular imports with that setup. The rule of thumb is to separate global modules at the higher level (e.g. src/schemas.py - for global schemas) and keep packages as isolated as possible, but be very explicit when importing other packages.

In cases, where package A (e.g. src/auth/schemas) requires package B (src/users/schemas) and wise-versa, solution is simple - rethink the structure you are building. Probably, you could combine them into one package or compliment them as a sub-package.