Django/Python Beginner: Error when executing python manage.py syncdb - psycopg2 not found

82,784

Solution 1

There seems to be a problem with your psycopg2 installation – Python does not find it. This is a Python installation problem, not a Django issue.

You can try to load it manually using the Python interpreter and see if it works:

$ python
>>> import psycopg2

If you get an ImportError exception, your installation is erroneous. To get a list of all directories Python looks for modules, use sys.path:

$ python
>>> import sys
>>> print sys.path

You can also add custom directories to Python's module search path by modifying the sys.path variable. Do this somewhere before the respective import statement(s):

import sys
sys.path.append("my-path")

# ...
import psycopg2

Solution 2

If you have pip installed, simply install the missing extension by running:

$ pip install psycopg2

Solution 3

For the record I got the same error for a different reason:

I had put

'ENGINE': 'django.db.backends.postgresql'

instead of

'ENGINE': 'django.db.backends.postgresql_psycopg2'

in settings.py

Solution 4

Follow the steps:

  1. Install python libraries on your OS:
    • python-dev
    • libpq-dev
  2. Execute the command to install the psycopg2 library:
    • easy_install psycopg2

source: http://initd.org/psycopg/install/

Solution 5

I had this issue recently after updating homebrew on OSX. psycopg2 was already listed in my virtualenv. I just reinstalled psycopg2 and it worked again:

pip install --force-reinstall psycopg2

Share:
82,784
wenbert
Author by

wenbert

http://blog.ekini.net

Updated on November 17, 2021

Comments

  • wenbert
    wenbert over 2 years

    I have Pythong2.6, psycopg2 and pgAdmin3 installed using Macports. My settings.py is:

    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.
    

    The error I get when I run python manage.py syncdb is:

    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
    

    Please note, I am a complete beginner in this stuff. I am originally a PHP-guy and trying out Python for a small personal project. Do I need to "turn on" Postgres?

    Also, when I sudo python manage.py runserver 8080 I get this error:

    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
    

    Please guide me. Any reply will be appreciated.

    Thanks,

    Wenbert!

  • wenbert
    wenbert almost 15 years
    The psycopg2 is found here: /opt/local/Library/Frameworks/Python.framework/Versions/2.6/‌​lib/python2.6/site-p‌​ackages/psycopg2-2.0‌​.12-py2.6.egg-info What do would you suggest that I do?
  • wenbert
    wenbert almost 15 years
    What do i do so that python looks in this path /opt/local/Library/Frameworks/Python.framework/Versions/2.6/‌​lib/python2.6/site-p‌​ackages instead of /Library/Frameworks/Python.framework/Versions/2.6/lib/python‌​2.6/site-packages
  • Ferdinand Beyer
    Ferdinand Beyer almost 15 years
    You can modify sys.path to add your own search directories. For django, you should do this in your settings.py. See my updated post for details.
  • wenbert
    wenbert almost 15 years
    Is it okay if I created a symbolic link to the python path created by Macports? Basically, I have something like this: Python.framework -> /opt/local/Library/Frameworks/Python.framework
  • Simon
    Simon almost 15 years
    I don' think it's a good idea to redirect the whole Python.framework since parts of Mac OS X might rely on it (plus, any software update might undo this or worse, be confused and install stuff elsewere). You might want to try symlinkling the actual packages inside the site-packages directory.
  • wenbert
    wenbert almost 15 years
    Thanks for this. I will create a symlink to the actual packages created by Macports.
  • Gavin Palmer
    Gavin Palmer almost 8 years
    I had an additional problem: stackoverflow.com/questions/11618898/…
  • JARC
    JARC almost 8 years
    Once psycopg2 is installed via the pkg manager you must run the pip command
  • tread
    tread almost 6 years
    Hmmm no mention of that engine in the db engine docs
  • Paul Kenjora
    Paul Kenjora about 5 years
    pip install psycopg2-binary is the one that worked.
  • NoName
    NoName about 4 years
    11 years later... I'm still waiting for an answer.