另一个Django“尚未加载应用”

时间:2018-07-11 16:30:02

标签: django python-3.x django-models pycharm conda

我对django和python都是新手,并且遇到了我不理解的错误。 runserver命令有效,我可以在localhost上输入我的网站。 但是,壳牌以某种方式被打破了。 据我所知,我在Conda虚拟环境上使用PyCharm。我正在使用macOS High Sierra。编辑:也使用Postgres。

尝试导入模型时出错

Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from catalog.models import Routines
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/fridalarsson/PycharmProjects/hapionline6/catalog/models.py", line 3, in <module>
    from senders.models import Senders
  File "/Users/fridalarsson/PycharmProjects/hapionline6/senders/models.py", line 4, in <module>
    class Senders(models.Model):
  File "/Users/fridalarsson/anaconda3/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/Users/fridalarsson/anaconda3/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/Users/fridalarsson/anaconda3/lib/python3.6/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.

有问题的模型如下:

from __future__ import unicode_literals
from django.db import models
from senders.models import Senders


class Routines(models.Model):
    routine_id = models.AutoField(primary_key=True)
    sender_id = models.ForeignKey(Senders, on_delete=models.CASCADE, default=1)
    in_routine = models.CharField(max_length=20)
    out_routine = models.CharField(max_length=20)
    mail_class = models.CharField(max_length=5)
    last_change = models.DateTimeField(auto_now=True)
    def __int__(self):
        return self.routine_id

另一个奇怪的是,当我使用常规的“ mac os”终端而不是PyCharm shell时,它将以如下错误消息开头:

/Users/fridalarsson/.conda/envs/hapionline6/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 63555 63556
import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend(['/Users/fridalarsson/PycharmProjects/hapionline6', '/Applications/PyCharm.app/Contents/helpers/pycharm', '/Applications/PyCharm.app/Contents/helpers/pydev'])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run("/Users/fridalarsson/PycharmProjects/hapionline6")
PyDev console: starting.
Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Django 2.0.5
Traceback (most recent call last):
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
    import psycopg2 as Database
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 19, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 6, in <module>
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate
    app_config.import_models()
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/fridalarsson/.conda/envs/hapionline6/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 "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Applications/PyCharm.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 19, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/models/base.py", line 114, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/models/base.py", line 315, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/models/options.py", line 205, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/fridalarsson/.conda/envs/hapionline6/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
    raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'

如果有人能帮助我,我将不胜感激!

3 个答案:

答案 0 :(得分:0)

我遇到了这个问题。对我来说,问题是psycopg2和六个正在发生冲突。我正在使用psycopg2 == 2.6.1和六个不匹配的版本。我指定使用六个1.10,并且psycopg2 == 2.7.1并成功。我会验证您的六个版本是否与psycopg2兼容。如果那不起作用,我来看看this。如果不确定如何检查版本,请使用pip freeze > requirements.txt写出requirements.txt。

答案 1 :(得分:0)

已解决: 正如Josewails指出的。该错误确实与psycopg2有关。

由于我使用的是Conda虚拟环境,因此能够通过“ Anaconda导航器” GUI /应用程序安装psycopg2。

然后添加了相关的libpg等。

现在,一切都可以在终端和“ runserver”中使用。

答案 2 :(得分:0)

另一种解决方案是使用重新安装psycopg。

pip install --global-option=build_ext \
            --global-option="-I/usr/local/opt/openssl/include" \
            --global-option="-L/usr/local/opt/openssl/lib" psycopg2

出于某种原因,它起作用