I'm creating a Database class for testing. This is the basic idea; build a database at instantiation, test with it, then delete it when the testing is done. I'd like to use the __del__
function to drop the schema when the object is destroyed.
def __init__(self):
dbinfo = dbinfo
with open('create_test_db_sql', 'r') as sql_file:
query = sql_file.read()
connection = self.connect_to_database()
cur = connection.cursor()
cur.execute(query)
connection.commit()
def connect_to_database(self):
return psycopg2.connect(dbinfo)
def __del__(self):
print("done")
conn = self.connect_to_database()
cur = conn.cursor()
cur.execute("DROP SCHEMA simulator_db_test CASCADE")
connection.commit()
Whenever I try to run this I get this python error
Exception ignored in: <function TestDatabase.__del__ at 0x0000017AC504E430>
Traceback (most recent call last):
File "Tests\TestDatabase.py", line 25, in __del__
File "venv\lib\site-packages\psycopg2\__init__.py", line 121, in connect
File "venv\lib\site-packages\psycopg2\extensions.py", line 163, in make_dsn
File "venv\lib\site-packages\psycopg2\extensions.py", line 163, in <listcomp>
File "venv\lib\site-packages\psycopg2\extensions.py", line 181, in _param_escape
ImportError: sys.meta_path is None, Python is likely shutting down
Not sure what this error means and there don't seem to be any explanations out there except for specific situations.
What does this error mean? It only occurs when I try to connect to the database. I've tried running other code such as print statements and those run as expected in the __del__
method.