I have a funny import error when using Inheritence in Python.
In a parent class I import the module sqlite3, in a child class I then try to use a sqlite3 function but I get an error saying “NameError: global name ‘sqlite3’ is not defined”. Why does this happen & how do I fix it?
The 2 classes are in separate files:
Parent.py
import sqlite3
class Parent:
def __init__(self):
self.create_database()
def create_database(self):
""" Virtual function to be overriden in child classes """
pass
...more class functions that use sqlite3 functions
Child.py
import Parent
class Child( Parent.Parent ):
def create_database(self):
self.db = sqlite3.connect("test.db") # Error occurs HERE
c = Child()
the sqlite3 module is imported into the Parent module hence you need to access it through that module
It is not directly imported into the Child module unless you tell python to do so, for example
Will give you access to all the members of the Parent module from within the Child module