I have met a weird issue while upgrading the Django v1.11 to v2.0, python 2 to 3. I can’t import the model class in one module to another module model file. It had never happened before. Does anyone know why and how to resolve it?
Thank
ImportError: cannot import name
There are a couple different reasons why this error can occur. Please provide more details. Show the line causing the error along with the name of the file it appears in.
(This is likely just the starting point, we may need to ask for a lot more to find the root issue.)
Did Django change the way to load an app in v2.0?
In v1.11 I only added the app name into INSTALLED_APPS like this
INSTALLED_APPS = [
"offers"
]
Do I need to create the apps.py file and OfferConfig class to like this
INSTALLED_APPS = [
"offers.apps.OffesConfig"
]
Here is traceback:
Traceback (most recent call last):
File "src/slots/manage.py", line 20, in <module>
execute_from_command_line(sys.argv)
File "/opt/kff/Whiplash-Server-Env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/opt/kff/Whiplash-Server-Env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/opt/kff/Whiplash-Server-Env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/opt/kff/Whiplash-Server-Env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/opt/kff/Whiplash-Server-Env/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/root/.pyenv/versions/3.6.15/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/opt/kff/Whiplash-Server/src/slots/wp_faction_mission/models.py", line 7, in <module>
from wp_blueprints.core.mixins import ExpiryDateMixin
File "/opt/kff/Whiplash-Server/src/slots/wp_blueprints/core/mixins.py", line 3, in <module>
from wp_blueprints.models import ModelExpiry
File "/opt/kff/Whiplash-Server/src/slots/wp_blueprints/models.py", line 5, in <module>
from wp_brick.models import BrickSettings
File "/opt/kff/Whiplash-Server/src/slots/wp_brick/models.py", line 724, in <module>
class Deeplink_shop(models.Model):
File "/opt/kff/Whiplash-Server/src/slots/wp_brick/models.py", line 728, in Deeplink_shop
from wp_battleshop.models import BattleShop
File "/opt/kff/Whiplash-Server/src/slots/wp_battleshop/models.py", line 4, in <module>
from wp_inventory.models import Currency
File "/opt/kff/Whiplash-Server/src/slots/wp_inventory/models.py", line 11, in <module>
from wp_grabbag.models import GrabBag
File "/opt/kff/Whiplash-Server/src/slots/wp_grabbag/models.py", line 8, in <module>
from wp_blueprints.core.mixins import ExpiryDateMixin
ImportError: cannot import name 'ExpiryDateMixin'
You have a circular import situation. You have a case where module “A” imports module “B”, which then imports module “C” which then tries to import module “A”.
Look at all the import statements above, you’ll see the following line twice:
And to address your other question about app definitions, both are valid. The AppConfig is necessary if there’s something you need to do that is handled by the AppConfig class. Otherwise both work exactly the same.