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

Abigail Solomon
Data Analysis Techdegree Student 4,441 PointsName Error: name 'User' is not defined. I cannot add my name to the userbase because of this error.
I'm currently working on the SQLAlchemy course and my file won't update with my name because of this name error, however I copied what Megan did during her demonstration and I'm not sure what could have been wrong. It's supposed to follow the format of the repr(self) function in the models.py, so I'm not sure what went wrong for me. I'm using the Treehouse console. This is the line of code that got the error: abi_user = User(name='Abigail', fullname='Abigail Solomon', nickname='Abi').
Here's the full code:
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///users.db', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key = True)
name = Column(String)
fullname = Column(String)
nickname = Column(String)
def __repr__(self):
return f'<User(name={self.name}, fullname={self.fullname}, nickname={self.nickname})>'
if __name__ == '__main__':
Base.metadata.create_all(engine)
abi_user = User(name='Abigail', fullname='Abigail Solomon', nickname='Abi')
print(abi_user.name)
print(abi_user.id)
session.add(abi_user)
print(session.new)
2 Answers

Travis Alstrand
Treehouse Project ReviewerIt's hard to tell without seeing everything, but if you're getting a User is not defined
error, that's in regards to that User class. I would ensure that you're importing it correctly.
If you continue to be stuck on this, could you please provide all the code in this file and a link to the video please?

Travis Alstrand
Treehouse Project ReviewerSo, I notice some formatting errors and that the double underscores were set as bold text due to markdown, so I'm not 100% sure as I can't tell if the line spacing / indentation with your code was messed up when pasting it in to the forum here. In the future, since indentation and formatting is so critical in Python, I would wrap your code in a markdown code block. There's a link to a markdown cheatsheet near your text box here when writing a post or comment. 👍
Anyways, I still wasn't sure of the video / challenge this was in regards to but I hope that this may help.
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///users.db', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
fullname = Column(String)
nickname = Column(String)
def __repr__(self):
return f'<User(name={self.name}, fullname={self.fullname}, nickname={self.nickname})>'
if __name__ == "__main__":
Base.metadata.create_all(engine)
abi_user = User(name='Abigail', fullname='Abigail Solomon', nickname='Abi')
print(abi_user.name)
print(abi_user.id)
session.add(abi_user)
print(session.new)
I believe you are currently a Techdegree student right? I would definitely move to asking questions in Slack if so. You'll get much quicker replies and it's much easier to communicate / share code there 👍

Abigail Solomon
Data Analysis Techdegree Student 4,441 PointsI am a Tech Degree student, I'll try to message you on Slack. I still get the name User is not defined error, although our code looks the same including indentations.

Travis Alstrand
Treehouse Project ReviewerYes I would reach out in the related unit
channel on Slack and provide a snapshot link to your workspace, or if this is something you're working on locally, push it to a GitHub repository and share that link instead.
If you need help creating a Workspace snapshot link, you can see how it's done in this video at the 5:30 mark 😃
Abigail Solomon
Data Analysis Techdegree Student 4,441 PointsAbigail Solomon
Data Analysis Techdegree Student 4,441 PointsI just edited the question with my entire code, hope this helps in you helping me out!