Search code examples
djangopostgresqlheroku

Heroku push of django app gets "No module named psycopg2.extensions"


I'm trying to push a django app to heroku and getting an error that I haven't seen anywhere in the heroku or stackoverflow forums. I'm using postgres.

I'm not sure where to proceed; I see nothing in any of the docs that says what is wrong. The main error is:

ImportError: No module named psycopg2.extensions

...but heroku's configuration is the one that is setting that as the ENGINE, I don't know how to get by it.

My conversation with heroku to make sure it knows about postgres

$ heroku addons:add heroku-postgresql:dev
Adding heroku-postgresql:dev on morning-crag-1585... done, v24 (free)
Attached as HEROKU_POSTGRESQL_OLIVE_URL
Database has been created and is available
 ! This database is empty. If upgrading, you can transfer
 ! data from another database with pgbackups:restore.
.
Use `heroku addons:docs heroku-postgresql:dev` to view documentation.
$ heroku pg:wait 
$ heroku config | grep HEROKU_POSTGRESQL
HEROKU_POSTGRESQL_OLIVE_URL: postgres://<blah-blah-blah>@ec2-<ip-address>.compute-1.amazonaws.com:5432/<blah-blah>
$ heroku pg:promote HEROKU_POSTGRESQL_OLIVE_URL
Promoting HEROKU_POSTGRESQL_OLIVE_URL to DATABASE_URL... done

My requirements.txt:

Django==1.4.2
wsgiref==0.1.2
dj-database-url==0.2.1

A snippet from my settings.py:

print "Got here before!\n"
try:
  import dj_database_url
  DATABASES = { 'default': dj_database_url.config(default='postgres://localhost') }
except:
  print "Unexpected error:", sys.exc_info()

print DATABASES

When I push to heroku, I get this from heroku logs:

heroku[slugc]: Slug compilation finished
heroku[web.1]: Starting process with command `python ./manage.py runserver 0.0.0.0:30550 --noreload`
app[web.1]: Got here before!
app[web.1]: 
app[web.1]: {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': '<blah blah>', 'HOST': 'ec2-<iip-address>.compute-1.amazonaws.com', 'USER': '<blah blah>', 'PASSWORD': '<blah>', 'PORT': 5432}}
app[web.1]: 
app[web.1]: Validating models...
app[web.1]: Traceback (most recent call last):
app[web.1]:   File "./manage.py", line 10, in <module>
app[web.1]:     execute_from_command_line(sys.argv)
app[web.1]:     utility.execute()
app[web.1]:     self.execute(*args, **options.__dict__)
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
app[web.1]:     self.fetch_command(subcommand).run_from_argv(self.argv)
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
app[web.1]:     self.inner_run(*args, **options)
app[web.1]:     self.validate(display_num_errors=True)
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_run
app[web.1]:     num_errors = get_validation_errors(s, app)
app[web.1]:     self.run(*args, **options)
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
app[web.1]:     output = self.handle(*args, **options)
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/validation.py", line 23, in get_validation_errors
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
app[web.1]:     from django.db import models, connection
app[web.1]:     return import_module('.base', backend_name)
app[web.1]:     __import__(name)
app[web.1]: ImportError: No module named psycopg2.extensions
app[web.1]:     return getattr(connections[DEFAULT_DB_ALIAS], item)
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/creation.py", line 1, in <module>
app[web.1]:     backend = load_backend(db['ENGINE'])
app[web.1]:     import psycopg2.extensions
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 69, in handle
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 80, in run
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
app[web.1]:     backend = load_backend(connection.settings_dict['ENGINE'])
app[web.1]:     from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 13, in <module>
heroku[web.1]: Process exited with status 1
heroku[web.1]: State changed from starting to crashed

Solution

  • You need to add the psycopg2 installation to your requirements.txt file. It is the database adapter - the way python interfaces with the PostgreSQL you indeed do not need to install because heroku does.

    Just add the line psycopg2==2.4.4 to your requirements.txt file.

    The database add-on addition itself, by the way, is not necessary. dj-database-url takes care of it automatically. Check out the heroku docs for a step-by-step guide.