cs50/python-cs50

Getting AttributeError: 'NoneType' object has no attribute 'WeakInstanceDict' while using SQL class in CS50's python library

tripathics opened this issue · 3 comments

AttributeError while using SQL class in CS50's Python library in my LInux System. (System information provided in bottom)

@dmalan sir I was doing favorites.py which you demonstrated in week7 SQL lecture in my offline IDE (vscode). Here it is:

favorites.py

import csv

from cs50 import SQL

open("shows.db", "w").close()
db = SQL("sqlite:///shows.db")

db.execute("CREATE TABLE shows (id INTEGER, title TEXT, PRIMARY KEY(id))")
db.execute("CREATE TABLE genres (show_id INTEGER, genre TEXT, FOREIGN KEY(show_id) REFERENCES shows(id))")

with open("Favorite TV Shows - Form Responses 1.csv", "r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        title = row["title"].strip().upper()

        id = db.execute("INSERT INTO shows (title) VALUES(?)", title)

        for genre in row["genres"].split(", "):
            db.execute("INSERT INTO genres (show_id, genre) VALUES(?, ?)", id, genre)

And here's the error I'm getting.

(py39) shyam@shyam-linux:~/Projects/CS50X/week7/fav$ python favorites.py 
Exception ignored in: <function SQL.__del__ at 0x7f821ae70f70>
Traceback (most recent call last):
  File "/home/shyam/Projects/virtual_env/py39/lib/python3.9/site-packages/cs50/sql.py", line 96, in __del__
  File "/home/shyam/Projects/virtual_env/py39/lib/python3.9/site-packages/cs50/sql.py", line 101, in _disconnect
  File "/home/shyam/Projects/virtual_env/py39/lib/python3.9/site-packages/sqlalchemy/orm/scoping.py", line 145, in remove
  File "/home/shyam/Projects/virtual_env/py39/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1745, in close
  File "/home/shyam/Projects/virtual_env/py39/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1784, in _close_impl
  File "/home/shyam/Projects/virtual_env/py39/lib/python3.9/site-packages/sqlalchemy/orm/session.py", line 1798, in expunge_all
AttributeError: 'NoneType' object has no attribute 'WeakInstanceDict'

Despite these errors, after checking the shows.db file I found the TABLES created and the data from csv file was successfully inserted into them.

I am very new to python and Computer Science. CS50X is my first course in the subject.

My system:

  • elementary OS 5.1.7 Hera (Built on Ubuntu 18.04.4 LTS) Linux 5.4.0-73-generic
  • Python version: Python 3.9.5
  • cs50 library version: 6.0.4
  • IDE: Visual Studio Code 1.56.2

Thank you for reporting this, @tripathics! Out of curiosity, are you seeing this issue on the refactor/scoped-session-fix branch? You should be able to install this version by running:

pip3 install git+https://github.com/cs50/python-cs50@refactor/scoped-session-fix

Thank you for reporting this, @tripathics! Out of curiosity, are you seeing this issue on the refactor/scoped-session-fix branch? You should be able to install this version by running:

pip3 install git+https://github.com/cs50/python-cs50@refactor/scoped-session-fix

No @kzidane sir. I installed it the usual way python3 -m pip install cs50

Ok I'll try installing from there and let you know

Thank you for reporting this, @tripathics! Out of curiosity, are you seeing this issue on the refactor/scoped-session-fix branch? You should be able to install this version by running:

pip3 install git+https://github.com/cs50/python-cs50@refactor/scoped-session-fix

@kzidane sir, this is not showing those above errors. Thank you very much!! It solved my problem