2

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.

Community
  • 1
  • 1
Myone
  • 1,103
  • 2
  • 11
  • 24
  • What is your `postgresql_psycopg2` version? Python 3.3 support comes in Psycopg 2.5 and laterer versions. You can sheck it from http://initd.org/psycopg/ – Mp0int Feb 02 '14 at 22:26
  • I tried to check my version both with python and in the command line (I used apt-get to install it), but it just told me that I had no module named psycopg2. – Myone Feb 03 '14 at 09:01
  • You probably install wrong `psycopg2` module with package manager.. See my answer. – Mp0int Feb 03 '14 at 09:17

5 Answers5

4

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.

Mp0int
  • 18,172
  • 15
  • 83
  • 114
  • Thanks, now I see why my apt-get command didn't work. Apparently easy_install automatically installs the python3 version. Thanks! – Myone Feb 03 '14 at 09:22
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
CDspace
  • 2,639
  • 18
  • 30
  • 36
0

If you are trying to use the default user, you have misspelled "postgres":

'USER': 'postgre',

The documentation at http://pythonhosted.org//psycopg2/install.html#installation recommends to install the package using repos. However, check your $PATH variable.

3manuek
  • 907
  • 8
  • 11
  • 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. – Myone Feb 03 '14 at 08:59
  • 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` – Mp0int Feb 03 '14 at 09:20
  • 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. – 3manuek Feb 05 '14 at 22:32
0

I solved the problem myself, however I'm not sure why this would be a good solution. For me, this command didn't install psycopg2 properly:

sudo apt-get install python-psycopg2

I couldn't get the version from the command line neither by importing the module in python3.3. So this is what I did:

sudo apt-get install libpq-dev

sudo easy_install psycopg2

And now it looks like this:

$ python3.3 manage.py runserver
Validating models...

0 errors found
February 03, 2014 - 08:54:13
Django version 1.6.1, using settings 'testprojekt.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

If someone knows why this solved the problem, feel free to comment. :)

Myone
  • 1,103
  • 2
  • 11
  • 24
  • 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 – Mp0int Feb 03 '14 at 09:22
0

I would recommend you to make your life easier and use virtualenv along with pip to install libraries. Why? Because being inside virtualenv you avoid mistaking python executables and libraries are installed for selected python version.

I personally keep one virtualenv for each project so I have only libraries that I really need. Also I know that along with updating system I won't end up with some new version of library which is not backward compatible. However I can still update libraries if needed by pip upgrade <library>. I can pack virtualenv and make requirement list for my project. I don't need sudo to install packages so it is also more safe.

Install virtualenv

sudo apt-get install python-virtualenv

Prepare virtualenv environment

sebastian@pc:~/test$ virtualenv -p python3.4 p34_virt_env
Running virtualenv with interpreter /usr/bin/python3.4
(...)

Check system default python interpreter

sebastian@pc:~/test$ python --version
Python 2.7.6

Activate virtualenv environment (see (p34_virt_env) virtualenv indicator?)

sebastian@pc:~/test$ source p34_virt_env/bin/activate

Check again python version - it changed

(p34_virt_env)sebastian@pc:~/test$ python --version
Python 3.4.0

You've got clean environment without libraries

(p34_virt_env)sebastian@pc:~/test$ pip list
pip (1.5.6)
setuptools (3.6)

Now you can pip install what you need and don't worry about if you have to install python3-xxx of python-xxx library.

(p34_virt_env)sebastian@pc:~/test$ pip install psycopg2
omikron
  • 2,745
  • 1
  • 25
  • 34