Я работаю над большим проектом Python и столкнулся с ситуацией, когда два или более модулей имеют циклические зависимости. Например, модуль A импортирует модуль B, а модуль B импортирует модуль A (прямо или косвенно). Это вызывает ошибку ImportError, поскольку интерпретатор не может разрешить зависимости. У меня есть следующая структура:
Модуль A
from module_b import func_b
def func_a():
print("This is function A")
func_b() ```
Module B
``` from module_a import func_a
def func_b():
print("This is function B")
func_a()```
When I try to run the code, I get the error: ```ImportError: cannot import name 'func_a' from partially initialized module 'module_a'```
My Questions related to this?
1. What is the best way to handle circular imports in Python, especially when modules need to reference each other?
2. Are there any design patterns I should follow to avoid circular dependencies in the first place?
3. Would breaking the modules into smaller, more focused submodules help prevent this issue, or is there a more Pythonic way to deal with circular imports in large projects?
Подробнее здесь: https://stackoverflow.com/questions/793 ... a-large-pr
Как я могу обрабатывать циклические зависимости в Python при использовании импорта в большом проекте? ⇐ Python
Программы на Python
1737374331
Anonymous
Я работаю над большим проектом Python и столкнулся с ситуацией, когда два или более модулей имеют циклические зависимости. Например, модуль A импортирует модуль B, а модуль B импортирует модуль A (прямо или косвенно). Это вызывает ошибку ImportError, поскольку интерпретатор не может разрешить зависимости. У меня есть следующая структура:
Модуль A
from module_b import func_b
def func_a():
print("This is function A")
func_b() ```
Module B
``` from module_a import func_a
def func_b():
print("This is function B")
func_a()```
When I try to run the code, I get the error: ```ImportError: cannot import name 'func_a' from partially initialized module 'module_a'```
My Questions related to this?
1. What is the best way to handle circular imports in Python, especially when modules need to reference each other?
2. Are there any design patterns I should follow to avoid circular dependencies in the first place?
3. Would breaking the modules into smaller, more focused submodules help prevent this issue, or is there a more Pythonic way to deal with circular imports in large projects?
Подробнее здесь: [url]https://stackoverflow.com/questions/79371233/how-can-i-handle-circular-dependencies-in-python-when-using-import-in-a-large-pr[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия