0
votes

I'm trying to get a heroku app deployed and unfortunately I am getting the following error. Essentially what the Heroku logs tell you to do to get a heroku database connection is the following (taken from here; https://devcenter.heroku.com/articles/heroku-postgresql)

import os
import psycopg2
import urlparse

urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])

conn = psycopg2.connect(
    database=url.path[1:],
    user=url.username,
    password=url.password,
    host=url.hostname,
    port=url.port
)

I started using gunicorn, but I couldn't understand the errors that I was getting because it was spawning like 10 workers, so I switched to waitress and I got the following error (from heroku logs):

2017-07-16T22:21:52.941072+00:00 app[web.1]: Exception in thread Thread-1:
2017-07-16T22:21:52.941072+00:00 app[web.1]: Traceback (most recent call last):
2017-07-16T22:21:52.941073+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 801, in __bootstrap_inner
2017-07-16T22:21:52.941073+00:00 app[web.1]:     self.run()
2017-07-16T22:21:52.941073+00:00 app[web.1]:   File "/app/.heroku/python/lib/python2.7/threading.py", line 754, in run
2017-07-16T22:21:52.941074+00:00 app[web.1]:     self.__target(*self.__args, **self.__kwargs)
2017-07-16T22:21:52.941074+00:00 app[web.1]:   File "/app/timed/inflation.py", line 28, in inflatemethod
2017-07-16T22:21:52.941075+00:00 app[web.1]:     urlparse.uses_netloc.append("postgres")
2017-07-16T22:21:52.941075+00:00 app[web.1]: AttributeError: 'function' object has no attribute 'uses_netloc'

So by default the heroku method that is in the docs is broken! That is bad! I'm not sure how to fix this, does anyone else have any ideas?

3
I'm getting the same error on Python 3 with gunicorn. Hmm... - Lachlan

3 Answers

1
votes

You haven't shown your actual code from inflation.py. In that code, you are almost certainly doing from urlparse import urlparse, instead of importing the module as in the example code.

0
votes

For python 3 the urlparse library is now urllib.parse

So the heroku sample is good for python 2 apps however for python3 apps the following changes should work:

import urllib.parse

urllib.parse.uses_netloc.append("postgres") url = urllib.parse.urlparse(os.environ["DATABASE_URL"])

0
votes

In Python 3, you need to use urllib.parse instead of urlparse.

import urllib.parse

urllib.parse.uses_netloc.append('postgres')
url = urllib.parse.urlparse(os.environ['DATABASE_URL'])

instead of

import urlparse

urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])