Can't import models on python console in Pycharm
Can't import models on python console in Pycharm
I am trying to import one of my models in Python Console in PyCharm and I am getting the following error
from damage.models import Damage
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:Program FilesJetBrainsPyCharm 2017.3.3helperspydev_pydev_bundlepydev_import_hook.py", line 20, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:projectsdjangodeyadamagemodels.py", line 2, in <module>
from django.contrib.auth.models import User
File "C:Program FilesJetBrainsPyCharm 2017.3.3helperspydev_pydev_bundlepydev_import_hook.py", line 20, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:projectsdjangodeyavenvlibsite-packagesdjangocontribauthmodels.py", line 2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:Program FilesJetBrainsPyCharm 2017.3.3helperspydev_pydev_bundlepydev_import_hook.py", line 20, in do_import
module = self._system_import(name, *args, **kwargs)
File "C:projectsdjangodeyavenvlibsite-packagesdjangocontribauthbase_user.py", line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:projectsdjangodeyavenvlibsite-packagesdjangodbmodelsbase.py", line 100, in __new__
app_config = apps.get_containing_app_config(module)
File "C:projectsdjangodeyavenvlibsite-packagesdjangoappsregistry.py", line 244, in get_containing_app_config
self.check_apps_ready()
File "C:projectsdjangodeyavenvlibsite-packagesdjangoappsregistry.py", line 127, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
Everything else in my project works fine.
I am able to import my models when i run
python manage.py shell
from command line or even PyCharm Terminal.
Can anyone help?
Thanks in advanced
Yes I do
C:projectsdjangodeyavenvScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.3helperspydevpydevconsole.py" 6550 6551 is the command when Python console starts. C:projectsdjangodeyavenv is the virtualenv– kpk
Aug 22 at 21:42
C:projectsdjangodeyavenvScriptspython.exe "C:Program FilesJetBrainsPyCharm 2017.3.3helperspydevpydevconsole.py" 6550 6551
C:projectsdjangodeyavenv
what do you get if you run
python manage.py runserver have you included the app in INSTALLED_APPS where the Damage model is defined?– Lingster
Aug 22 at 22:09
python manage.py runserver
python manage.py runserver executes fine and I also included the app in INSTALLED_APPS– kpk
Aug 23 at 5:47
python manage.py runserver
1 Answer
1
Unfortunately, I don't think it is possible to work with Django outside of the python manage.py runserver initialisation. What happens when you run this in the terminal, do you receive any errors?
Django
python manage.py runserver
It's actually possible to run Django outside of manage.py, in your script you just have to include:
from django.conf import settings from django.core.wsgi import get_wsgi_application from django.core.management import call_command application = get_wsgi_application() and also add DJANGO_SETTINGS_MODULE environment variable so django knows where to load the settings.py file from ...– Lingster
Aug 22 at 22:15
from django.conf import settings from django.core.wsgi import get_wsgi_application from django.core.management import call_command application = get_wsgi_application()
@Lingster Yes, outside of the normal configuration setup - it's possible I guess. :) I'm assuming no prior or very beginner level Django experience for this user.
– user10261970
Aug 22 at 22:18
DJANGO_SETTINGS_MODULE is defined in project settings. I define it manually by
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "deya.settings"). I tried also import django and 'django.setup()'. After all these i am getting the error 'RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.`– kpk
Aug 23 at 6:32
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "deya.settings")
import django
@kpk Hi, the deflation of app label is certainly because you’re running it outside of the python manage.py runserver.
– user10261970
Aug 23 at 6:40
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Do you have a virtualenv set up?
– pfcodes
Aug 22 at 21:38