【问题标题】:Problems with psycopg2 installation/Python pathpsycopg2 安装/Python 路径的问题
【发布时间】:2012-06-22 03:23:49
【问题描述】:

我在我的 django app 文件夹中创建了一个 virtualenv,并在那里安装了我所有的 python 依赖项。我的 Django 项目被称为主题,所以在主题文件夹中有一个包含 python 库的 venv 文件夹。 如果我运行python manage.py syncdb,它会抛出Error was: No module named postgresql_psycopg2.base。这是回溯:

(venv)app@domain-VirtualBox:~/dj/theme$ python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_manager(settings)
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 459, in execute_manager
    utility.execute()
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/core/management/sql.py", line 6, in <module>
    from django.db import models
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/app/dj/theme/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 51, in load_backend
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'postgresql_psycopg2' isn't an available database backend.
Try using django.db.backends.postgresql_psycopg2 instead.
Error was: No module named postgresql_psycopg2.base

但问题是,如果我尝试从 python 命令行导入 psycopg2,它会起作用:

(venv)app@domain-VirtualBox:~/dj/theme$ python
Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
>>> quit()

这是我的 python 路径:

>>> import sys
>>> print sys.path
['', '/home/app/dj/theme/venv/local/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/home/app/dj/theme/venv/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/home/app/dj/theme/venv/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg', '/home/app/dj/theme/venv/lib/python2.7/site-packages/pip-1.1-py2.7.egg', '/home/app/dj/theme/venv/lib/python2.7', '/home/leonsas/dj/theme/venv/lib/python2.7/plat-linux2', '/home/app/dj/theme/venv/lib/python2.7/lib-tk', '/home/app/dj/theme/venv/lib/python2.7/lib-old', '/home/app/dj/theme/venv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/home/app/dj/theme/venv/local/lib/python2.7/site-packages', '/home/app/dj/theme/venv/lib/python2.7/site-packages']
>>> quit()

还有whereis python

(venv)app@domain-VirtualBox:~/dj/theme$ whereis python
python: /usr/bin/python2.7 /usr/bin/python /usr/bin/python2.7-config /etc/python2.7 /etc/python /usr/lib/python2.7 /usr/bin/X11/python2.7 /usr/bin/X11/python /usr/bin/X11/python2.7-config /usr/local/lib/python2.7 /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz

我相信问题出在那儿,但我不知道如何解决它。有什么想法吗?

【问题讨论】:

    标签: python django virtualenv pythonpath


    【解决方案1】:

    尝试遵循django.core.exceptions.ImproperlyConfigured 异常的建议: 在您的设置中使用'django.db.backends.postgresql_psycopg2' 而不是'postgresql_psycopg2' 中的DATABASES

    【讨论】:

    • 是的,它奏效了。我曾尝试使用 django.db.backends.postgresql_psycopg2(不带引号),但正确的方法是将其作为字符串传递,因此 'django.db.backends.postgresql_psycopg2' 起作用了。
    【解决方案2】:

    您需要先将 PostgreSQL 数据库服务器连接到 postgresql_psycopg2。

    按照本指南:https://www.postgresqltutorial.com/postgresql-python/connect/

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    猜你喜欢
    • 2020-09-16
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多