Managing all modules imports in a single file in Python/Django project, while avoiding circular imports

I’m working on a Django project that has numerous imported modules across various files, sometimes totaling up to 50 lines of imports per page. To reduce cluttering, I created a single file, "imports.py", to centralize my imports.

Here’s a brief example of what it looks like:

from datetime import date, datetime
from typing import Any, Callable, Optional, Final, Type, TypeAlias, cast, Iterable
from functools import wraps
from cryptography.fernet import Fernet
from requests.auth import HTTPBasicAuth
from requests.models import Response
from PIL import Image, UnidentifiedImageError
from smtplib import SMTPRecipientsRefused
from dotenv import load_dotenv
from django.db import models, IntegrityError
...
# a lot more

__all__ = [
    'datetime', 'Any', 
    'Callable', 'Optional', 'Final', 'Type', 'TypeAlias', 'cast', 'Iterable', 'wraps', 
    'Fernet', 'HTTPBasicAuth', 'Response', 'Image', 'UnidentifiedImageError', 
    'SMTPRecipientsRefused', 'load_dotenv', 'models', 'IntegrityError',......]

Then, in other files, I import everything from imports.py like this:

from bi_app.py.imports import *

While I know this might be unconventional, I find it more organized. This method works well for external modules and also for Linters, but when I try to include imports for my own project files, I often run into circular import issues.

My question is: Is there a way to combine all imports from my own files, into a single file without causing circular imports? Thanks for your help

Вернуться на верх