Heroku App Deployement - cannot download psycopg2

I am trying to create a small app in Python Flask. To deploy the app non-locally I’m trying to use Heroku. To connect the database up I need to install the package psycopg2 but it doesn’t work. How can I get around this problem and if not will psycopg2-binary work instead.

link to codecademy lesson:
https://www.codecademy.com/courses/learn-flask/articles/deploying-a-flask-app

Output in terminal:
error: subprocess-exited-with-error

  pg_config is required to build psycopg2 from source.  Please add the directory
  containing pg_config to the $PATH or specify the full executable path with the
  option:
  
  python setup.py build_ext --pg-config /path/to/pg_config build ...
  
  or with the pg_config option in 'setup.cfg'.
  
  If you prefer to avoid building psycopg2 from source, please install the PyPI
  'psycopg2-binary' package instead.
  
  For further information please check the 'doc/src/install.rst' file (also at
  <https://www.psycopg.org/docs/install.html>).

They provide you with a solution. Try that instead.(?)

Unfortunately this doesn’t seem to make it work. I’m asking because I’ve followed everything else in the tutorial to the t. The only other thing that didn’t work was the db.create_all() right at the end of the tutorial to connect up the PostgreSQL with Heroku (I think this may be because I had to install psycopg2-binary instead of psycopg2, as that was also related to connecting PostgreSQL):

heroku run python

from app import db
db.create_all()

last 3 lines of the output:
File “/app/.heroku/python/lib/python3.10/site-packages/sqlalchemy/util/langhelpers.py”, line 343, in load
raise exc.NoSuchModuleError(
sqlalchemy.exc.NoSuchModuleError: Can’t load plugin: sqlalchemy.dialects:postgres

what version of SQLAlchemy did you download?

I ask b/c of this:

https://help.heroku.com/ZKNTJQSK/why-is-sqlalchemy-1-4-x-not-connecting-to-heroku-postgres

More importantly, in the app.config["SQLALCHEMY_DATABASE_URI"] = part

" The URI should start with postgresql:// instead of postgres:// . SQLAlchemy used to accept both, but has removed support for the postgres name."
See also: sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres - Codegrepr

1 Like

Thank you for your help lisalisaj. After a lot of figuring out I’ve managed to fix it :slight_smile:

2 Likes