我使用Macports安装了Pythong2.6,psycopg2和pgAdmin3。我的settings.py是:
DATABASE_ENGINE = 'postgresql_psycopg2' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'mysite' # Or path to database file if using sqlite3.
DATABASE_USER = 'postgres' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
运行python manage.py syncdb时出现的错误是:
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_manager(settings)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 221, in execute
self.validate()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/validation.py", line 22, in get_validation_errors
from django.db import models, connection
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 41, in <module>
backend = load_backend(settings.DATABASE_ENGINE)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 17, in load_backend
return import_module('.base', 'django.db.backends.%s' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 22, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
请注意,我是这个东西的完全初学者。我原来是一个PHP人,并尝试用于一个小型的个人项目。我需要“打开”Postgres吗?
另外,当我sudo python manage.py runserver 8080时 我收到这个错误:
Validating models...
Unhandled exception in thread started by <function inner_run at 0x1242670>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/commands/runserver.py", line 48, in inner_run
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/base.py", line 249, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/management/validation.py", line 22, in get_validation_errors
from django.db import models, connection
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 41, in <module>
backend = load_backend(settings.DATABASE_ENGINE)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/__init__.py", line 17, in load_backend
return import_module('.base', 'django.db.backends.%s' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 22, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
请指导我。任何回复将不胜感激。
谢谢,
Wenbert!
答案 0 :(得分:35)
您的psycopg2
安装似乎存在问题 - Python找不到它。这是一个Python安装问题,而不是Django问题。
您可以尝试使用Python解释器手动加载它并查看它是否有效:
$ python
>>> import psycopg2
如果您收到ImportError
异常,则表明您的安装有误。要获取Python查找模块的所有目录的列表,请使用sys.path
:
$ python
>>> import sys
>>> print sys.path
您还可以通过修改sys.path
变量将自定义目录添加到Python的模块搜索路径中。在相应的import
语句之前的某处执行此操作:
import sys
sys.path.append("my-path")
# ...
import psycopg2
答案 1 :(得分:27)
如果您安装了pip
,只需运行以下命令安装缺少的扩展程序:
$ pip install psycopg2
答案 2 :(得分:14)
对于记录,出于不同原因我得到了同样的错误:
我放了
'ENGINE': 'django.db.backends.postgresql'
而不是
'ENGINE': 'django.db.backends.postgresql_psycopg2'
settings.py
中的
答案 3 :(得分:7)
按照以下步骤操作:
python-dev
libpq-dev
psycopg2
库:
easy_install psycopg2
答案 4 :(得分:4)
我意识到我没有安装psycopg2
aptitude install python-psycopg2
像魅力一样工作
答案 5 :(得分:3)
虽然你安装了它,但Python显然找不到模块psycopg2。这通常是由于模块不在Python的路径中。看看您是否可以在/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
中找到名为psycopg2的文件夹。如果它不在那里,MacPorts会告诉你它放在哪里psycopg2?如果你可以找到它,只需将它移动到site-packages
目录,你应该没问题。
答案 6 :(得分:2)
对我来说,确实安装了psycopg2,但没有安装到Django运行的virtualenv中。这两个步骤修复了它:
sudo apt-get build-dep python-psycopg2
sudo /opt/myenv/bin/pip install psycopg2
答案 7 :(得分:1)
是的蒂姆的回答也适用于我。它没有前缀'django.db.backends'。也。但请记住创建settings.py中提到的数据库或模式:
DATABASE_NAME = 'your_db_name'
手动使用您的数据库客户端,因此当您运行'python manage.py syncdb'时,您不会遇到同样的问题。我被卡住了,因为我没有手动创建它。我得到同样的问题可能是因为我使用了buildout。
答案 8 :(得分:1)
我收到了同样的错误,但是因为我的virtualenv只有python26 ./manage.py runserver
和python
个可执行文件(因此使用了系统python2.6
),所以我正在使用python26
,没有安装psycopg2
答案 9 :(得分:0)
答案 10 :(得分:0)
我在OSX上更新自制程序后最近遇到了这个问题。 psycopg2已经在我的virtualenv中列出了。 我刚刚重新安装了psycopg2,它又恢复了工作:
pip install --force-reinstall psycopg2
答案 11 :(得分:0)
我在Windows上安装过psycopg2,但是64位版本。所以我的修复是从here下载32位,然后在PowerShell中激活我的虚拟环境,我的修复是:
pip uninstall psycopg2
easy_install C:\WHEREVER_I_DOWNLOADED_IT\psycopg2-2.6.1.win32-py3.4-pg9.4.4-release.exe
(Windows Python 3.4安装程序会自动安装easy_install和pip,而pip是删除软件包的最简单方法,即使最初使用easy_install安装也是如此。)
答案 12 :(得分:0)
这已经帮助了我:
我刚刚将ENV的PostgreSQL bin路径添加到了它,它能够精确所需的dll: C:\ Program Files(x86)\ PostgreSQL \ 9.4 \ bin
https://groups.google.com/forum/#!topic/robotframework-users/qLq9KvHe1wE
答案 13 :(得分:0)
与此处相同,但可能在当前项目中丢失了,因此我导航到主项目目录并安装了它。
pip install psycopg2
成功了
答案 14 :(得分:0)
使用django postgresql作为后端在heroku部署中会出现此错误,只需将psycopg2添加到您的requirements.txt文件中
答案 15 :(得分:0)
当您忘记激活虚拟环境时,也会发生此错误。