Python Forum
NameError :name 'name' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError :name 'name' is not defined
#1
taking a course at the moment which I why do follow examples, but getting the following Name error: name 'name' not defined.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://suz:COCOchanel88@localhost:5432/suzb'


db = SQLAlchemy(app)

class Person(db.Model):
  __tablename__ = 'persons'
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(), nullable=False)

db.create_all()

@app.route('/')
def index():
    person = Person.query.first()
    return 'Hello World!' 
when Ieventually run it in my terminal thats when I get the "nameError: name 'name' is not defined"
Reply
#2
Please post the full text of the error message, it's not clear to me where that error would be occurring.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Error:
>>> import flask_hello_app Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/ThaisB/class-demos/flask_hello_app.py", line 4, in <module> app = Flask(name) NameError: name 'name' is not defined
That is the error message I get and please need help on how to tackle that. Thank you, Craig, for the correction.
Reply
#4
import like this will not work.
Here some step for this.
# app.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' # //// 4 on Linux
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)
class Person(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(), nullable=False)

@app.route('/')
def index():
    return 'Hello World!'

if __name__ == '__main__':
   app.run(debug=True)
Se what i have changed like no db.create_all() in code.
I use sqlite so so can test without log in to eg postgresql.

Now for command line in same folder as app.py.
This is one one time operation here we create the database.
>>> from app import db
>>> from app import Person
>>> db.create_all()
>>> exit()
Now have create database teste.db.
Test that it work.
>>> from app import db
>>> from app import Person

>>> guest = Person(name='Kent')
>>> guest1 = Person(name='Tom')

>>> db.session.add(guest)
>>> db.session.add(guest1)
>>> db.session.commit()
>>> exit()
Look at test.db with sqlite command line tool.
λ sqlite3 test.db
SQLite version 3.28.0 2019-04-16 19:49:53
Enter ".help" for usage hints.
sqlite> .mode column
sqlite> .headers on

sqlite> select * from Person;
id          name
----------  ----------
1           Kent
2           Tom
Reply
#5
NameError: global name '---' is not defined
Other names are defined within the program (such as variables). If Python encounters a name that it doesn't recognize, you'll probably get this error. Some common causes of this error include: Forgetting to give a variable a value before using it in another statement.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: Name 'path' is not defined aniyanetworks 9 59,540 Jun-29-2018, 03:21 PM
Last Post: gontajones
  NameError: name 'download' is not defined ntdropper 3 11,206 Jan-13-2018, 07:18 AM
Last Post: snippsat
  NameError: name 'bsObj' is not defined Blue Dog 14 15,487 Oct-24-2016, 08:34 AM
Last Post: Blue Dog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020