Django runserver error (psycopg2)

11,539

Solution 1

I check my local installation of psycopg2

apt-cache show python-psycopg2

Package: python-psycopg2

Priority: optional

Section: python

Installed-Size: 635

...

Source: psycopg2

Version: 2.4.5-1

Depends: python2.7, python (>= 2.7.1-0ubuntu2), python (<< 2.8), libc6 (>= 2.14), libpq5 (>= 8.3~)

So, python-psycopg2 installs you the version which is not compatible with python 3.x But also there is a a package named python3-psycopg2 When I check it, it looks like it is also Version: 2.4.5-1 but descriptive text tells it is compatible with python 3.2

apt-cache show python3-psycopg2

Package: python3-psycopg2

Priority: optional

Version: 2.4.5-1

Provides: python3.2-psycopg2

Depends: python3 (>= 3.2), python3 (<< 3.3), libc6 (>= 2.14), libpq5 (>= 8.3~)

Also, package sizes are different. Probably python3-psycopg2 is what you need, it was only mis-versioned on the documents.

Solution 2

I was facing this problem and solved following the process are given below:

Install pip3:

sudo apt-get install python3-pip

Install virtualenv:

sudo pip3 install virtualenv

Install posgresql dependency:

sudo apt-get install python3-dev libpq-dev

Configure virtualenv:

mkdir django
cd django
virtualenv -p /usr/bin/python3.4 venv
source venv/bin/activate

Install django:

pip3 install django

Install psycopg2:

pip3 install psycopg2
Share:
11,539
Myone
Author by

Myone

Updated on June 04, 2022

Comments

  • Myone
    Myone almost 2 years

    I'm trying to do this:

    python3.3 manage.py runserver
    

    In my Django project folder. I get this error message:

      File "/usr/local/lib/python3.3/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 25, in <module>
        raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
    django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module  named 'psycopg2'
    

    My settings in settings.py are the following:

    DATABASES = {
        'default': {
            'ENGINE':'django.db.backends.postgresql_psycopg2',
            'NAME': 'test_databas',
            'USER': 'postgre',
            'PASSWORD': 'mypassword',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }
    

    I have already downloaded psycopg2 with Python3.3. The file /usr/local/lib/python3.3/site-packages/django/db/backends/postgresql_psycopg2/base.py also exists. However, Python doesn't think I have a module named psycopg2. I have tried this:

    $ python3.3
    Python 3.3.3 (default, Feb  2 2014, 14:32:49) 
    [GCC 4.6.3] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import psycopg2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named 'psycopg2'
    >>> 
    

    I have also changed my sys.path as suggested here.

    I'm new to this and would be very grateful for some help.

  • Myone
    Myone over 10 years
    Yes I was using the default user. Thanks! I have already changed my path variable and that didn't help unfortunately. However I somehow managed to solve the problem myself using easy_install instead.
  • FallenAngel
    FallenAngel over 10 years
    That can not be the reason because error says it can not import related module. In your situation, Django will throw Database Connection based error, not ImportError
  • Myone
    Myone over 10 years
    Thanks, now I see why my apt-get command didn't work. Apparently easy_install automatically installs the python3 version. Thanks!
  • FallenAngel
    FallenAngel over 10 years
    Probably easy_install installed the latest version, while linux package index contains 2 packages, one for 3.x and the other is for 2.x
  • 3manuek
    3manuek over 10 years
    That's why I commented after about the installation process. I didn't say that the user name fix is the reason. Sorry the lack of detail.