5

I'm having trouble using the following lines of code from https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-python

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'm using Python 3.6.2

In my Heroku logs I'm seeing:

ModuleNotFoundError: No module named 'urlparse'

Any help would be much appreciated!!

okcapp
  • 405
  • 1
  • 4
  • 15

1 Answers1

19

urlparse has been moved to a new module in python 3

from urllib.parse import urlparse

Read more here: https://docs.python.org/3.0/library/urllib.parse.html

Vikash Singh
  • 13,213
  • 8
  • 40
  • 70
  • 1
    Thanks for the response! When I made the change you suggested I received a different error. Now, in the Heroku logs, I'm seeing: AttributeError: 'function' object has no attribute 'uses_netloc' – okcapp Aug 05 '17 at 05:19
  • That's a bug in your code. You can post a different question with the updated code and heroku error log trace.. – Vikash Singh Aug 05 '17 at 05:21