在ubuntu os中的virtualenv中安装psycopg2

时间:2014-09-17 21:46:20

标签: python django postgresql psycopg2

我使用了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

当我在我的虚拟环境中安装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

2 个答案:

答案 0 :(得分:1)

根据您的问题和您的一条评论的组合,当您运行sudo pip install psycopg2时,它似乎正在使用系统安装,因为它在/ usr / lib / python2中引用了 psycopg2 .7 / dist-packages ,这与您尝试运行 Django 的环境不同。

您的权限错误来自 Django 环境,似乎可能是由于未使用sudo(其中包括某些 C 编译需求)在 psycopg2 安装期间发生,因此通常需要sudo,而不是许多其他包。)

我建议您在运行sudo命令时明确地参考 virtualenv 安装 pip 二进制文件。

即:sudo <virtualenv_root>/bin/pip install psycopg2

答案 1 :(得分:0)

这是一个旧线程,但是在基于Ubuntu的系统上将psycopg2 pip安装到virtualenv时,有些人可能会遇到Python.h not found类型的错误。

通过运行python -V virtualenv内部检查您的virtualenv python版本。

假设它是Python 3.8.0,请尝试通过以下方式将libpython3.8-dev 安装到系统(不是virtualenv):

sudo apt-get install libpython3.8-dev

可能与其他版本相似。

那为我解决了。