Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Trying to set up python Class object: AttributeError: 'PgCreate' object has no attribute ''

With the following code:

import psycopg2
import sys

class PgCreate:

    def __init__(self, host, dbname, user, password):
        self.host = ""
        self.dbname = "pg_main"
        self.user = "postgres"
        self.pasword = "postgres"

    def connectdb(self):
        con = None
        con = psycopg2.connect("host='%s' dbname='%s' user='%s' password='%s'" % (self.host, self.dbname, self.user, self.password))
        cur = con.cursor()
myobject = PgCreate('host', 'dbname', 'postgres', 'password')

I am trying to set up and test an object connectdb with by printing myobject.con and hopefully printing the result of psycopg2.connect

I am new to creating classes so the tet print is just me trying to verify that it's working but i get the following error:

Traceback (most recent call last):
  File "createdb.py", line 39, in <module>
AttributeError: 'PgCreate' object has no attribute 'con'

Hey Aaron, if you're looking to get the result from con in your connectdb method.. return it and print it to the screen. myobject is an instance of Pgcreate which does not have the attribute con.. which means you cannot access it through dot notation (myobject.con)

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

As rainmaker mentioned, "con" is not an attribute of the class. If you wanted to make it so, you could alter your code:

import psycopg2
# import sys

class PgCreate:

    def __init__(self, host="", dbname="pg_main",
                 user="postgres", password="password"):
        self.host = host
        self.dbname = dbname
        self.user = user
        self.password = password  # corrected 'pasword' typo
        self.con = None

    def connectdb(self):
        self.con = psycopg2.connect(
            f"host='{self.host}' dbname='{self.dbname}' "
            f"user='{self.user}' password='{self.password}'")
        self.cur = self.con.cursor()

myobject = PgCreate()  # No need for arguments if using defaults
print(f"connection {myobject.con}")  # originally None
myobject.connectdb()  # make the connection
print(f"connection {myobject.con}")

I've also modified the __init__ method to accept arguments. In the original code, the arguments didn't matter since the attributes were hard coded inside the method. I changed the string formatting to the newer f string format to show the cleaner look.

Post back if you need more help. Good luck!!

Thank you! that was a very helpful display of code. I was able to get a connection and output the results for my understanding.