错误是:没有名为 postgresql.base 的模块

Posted

技术标签:

【中文标题】错误是:没有名为 postgresql.base 的模块【英文标题】:Error was: No module named postgresql.base 【发布时间】:2016-06-08 22:29:36 【问题描述】:

我正在尝试将 Django 应用程序迁移到 postgresql,但我无法让 Django 识别实际安装了 postgres。我已经将它安装在我的机器上,并且它在我的计算机上运行良好,但是当我尝试在我的 settings.py 中将其设置为默认值时,我收到了错误:

我已经完成了两个关于让 Django here 和 here 运行 postgresql 的演练,但我仍然收到此错误。我应该在我的虚拟环境中安装 postgresql 吗?我试过这样做,但没有运气。

settings.py

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'pygamers',
        'USER': 'myusername',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
    

完整的追溯

Unhandled exception in thread started by <function wrapper at 0x10ab64410>
Traceback (most recent call last):
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/userprofile/models.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/db/models/base.py", line 108, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/db/models/base.py", line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/db/utils.py", line 212, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/tomeldridge/Desktop/Misc/Hobbies/Programming/Projects/Pygamers/venv/lib/python2.7/site-packages/django/db/utils.py", line 135, in load_backend
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'sqlite3'
Error was: No module named postgresql.base

【问题讨论】:

你安装 psycopg2 了吗? 是的,我已经安装了psycopg2,可以导入成功。 你能显示更多的回溯吗?错误源自何处? @Evert 添加了问题的完整回溯 什么版本的 django? 【参考方案1】:

所以我得到了与上面几乎相同的堆栈跟踪。所以我运行了以下内容:

pip install psycopg2

之后堆栈跟踪更改为

django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named postgresql.base

然后我打开了我的 settings.py 文件并按照上面堆栈跟踪中的建议更改了数据库后端

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'database',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'database',
        'USER': 'username',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    

应用程序成功连接!从How to setup PostgreSQL Database in Django?得到信息

【讨论】:

你运行的是什么版本的 Django。因为 Django 1.9(根据 OP 的版本)处理 both versions of the engine name。 OP 还安装了 psycopg2,但您没有。因此,此答案不适用于 OP 的情况(除非 OP 在某处犯了错误)。 Django 版本 1.8.12

以上是关于错误是:没有名为 postgresql.base 的模块的主要内容,如果未能解决你的问题,请参考以下文章

视图中的 Django 导入错误 - 没有名为“x”的模块

错误 - NoSuchBeanDefinitionException:没有名为“entityManagerFactory”的bean可用

尽管结构已序列化,但提升错误“结构没有名为'序列化'的成员”

错误:ModuleNotFoundError:没有名为“图像”的模块

manage.py上没有名为'portfoliodjango'的模块错误

检查错误--as-cran“没有名为...的包”