简体   繁体   English

无法在 Pycharm 中的 python 控制台上导入模型

[英]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:我正在尝试在 PyCharm 中的 Python 控制台中导入我的一个模型,但出现以下错误:

    from damage.models import Damage
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 20, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\projects\django\deya\damage\models.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "C:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 20, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\projects\django\deya\venv\lib\site-packages\django\contrib\auth\models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "C:\Program Files\JetBrains\PyCharm 2017.3.3\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 20, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\projects\django\deya\venv\lib\site-packages\django\contrib\auth\base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "C:\projects\django\deya\venv\lib\site-packages\django\db\models\base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\projects\django\deya\venv\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "C:\projects\django\deya\venv\lib\site-packages\django\apps\registry.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.从命令行甚至 PyCharm 终端。

How can I solve this?我该如何解决这个问题?

Unfortunately, I don't think it is possible to work with Django outside of the python manage.py runserver initialisation.不幸的是,我认为在python manage.py runserver初始化之外使用Django是不可能的。 What happens when you run this in the terminal, do you receive any errors?当您在终端中运行它时会发生什么,您是否收到任何错误?

This is what solve the issue for me.这就是为我解决问题的原因。

  • Once you're project is running using the IDE interface.使用 IDE 接口运行项目后。
  • Open the python console and:打开 python 控制台并:
    1. import django
    2. from {YOPUR MODEL} import {MODEL_NAME, ...}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM