【发布时间】:2014-09-17 21:46:20
【问题描述】:
我使用的是 virtualenv,我正在尝试设置 postgresql。我的系统上安装了 psycopg2,但我的虚拟环境似乎无法识别 psycopg2。是否可以将系统范围的 psycopg2 文件复制到我的 django 项目目录中,或者这是一种不好的方法?
这是我的 django 项目中 settings.py 文件的相关部分。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_db' ,
'USER': 'myusername' ,
'PASSWORD': 'mypassword' ,
'HOST': 'localhost' ,
'PORT': '' ,
}
}
当我运行 python manage.py migrate 时,我收到此错误:
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
class Permission(models.Model):
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/models/base.py", line 125, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/models/base.py", line 300, in add_to_class
value.contribute_to_class(cls, name)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named psycopg2
当我在我的虚拟环境中执行 pip install psycopg2 时,我收到以下错误:
Exception:
Traceback (most recent call last):
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/commands/install.py", line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/req.py", line 1153, in prepare_files
location = req_to_install.build_location(self.build_dir, not self.is_download)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/req.py", line 218, in build_location
_make_build_dir(build_dir)
File "/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/req.py", line 1527, in _make_build_dir
os.makedirs(build_dir)
File "/uwsgi-tutorial/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/uwsgi-tutorial/build'
Storing debug log for failure in /home/user/.pip/pip.log
【问题讨论】:
-
您是否尝试过使用
pip install psycopg2在您的 virtualenv 中重新安装 psycopg2? -
是的,我有。我收到此错误:异常:回溯(最近一次调用最后一次):文件“/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/basecommand.py”,第 122 行,主要状态 = self.运行(选项,args)文件“/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/commands/install.py”,第 278 行,运行中 require_set.prepare_files(finder,force_root_egg_info=self.bundle , bundle=self.bundle) 文件“/uwsgi-tutorial/local/lib/python2.7/site-packages/pip/req.py”,第 1153 行,在 prepare_files location = req_to_install.build_location(self.build_dir, not self .is_download)....
-
当我执行 sudo pip install psycopg2 时,我得到以下信息:要求已经满足(使用 --upgrade 升级): /usr/lib/python2.7/dist-packages 中的 psycopg2 清理。 ..当我再次尝试迁移时,仍然是同样的错误。
-
您的 Postgres 用户很可能没有正确的权限。它们设置正确吗?您可以使用与
settings.py相同的用户登录psql吗?
标签: python django postgresql psycopg2