I'm trying to implement an abstract class in python (actually in a django app) and am running up against this madness:
>python concreteChildClass.py
Traceback (most recent call last):
File"concreteChildClass.py, line 1, in <module>
from abc_base import AbstractBaseClass
ImportError: No module named abc_base
I've boiled down my problem to these files that reproduce the error in my env:
abstractBaseClass.py
import abc
class abstractBaseClass(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def load(self, input):
"""Retrieve data from the input source and return an object."""
return
concreteChildClass.py
from abc_base import AbstractBaseClass
class ConcreteChildClass(object):
def load(self, input):
return input.read()
Here is my python version info
>>> import sys
>>> print sys.version
2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2]
I'm fairly new to python (as this question may make painfully obvious), but I can't figure out how 'abc' would be found but not 'abc_base'. My reading and googling has lead me to no answers on this one. Thanks in advance and apologies if this is a silly question.
I'm assuming that you're following this tutorial?
The mistake that you've made (and to be fair, the tutorial is unclear on this), is assuming that abc_base
is the name of some module that lives inside the standard library.
Rather, it just happens to be the name of the very first python file in the tutorial, in which the PluginBase
class is defined.
To adapt the code to work for you, you need to import from whatever file contains your desired base class, rather than from abc_base
.
Note: Because the class names and file names are identical in your example, I went ahead and changed the file names to make it clearer what's happening and what you need to import:
base.py
import abc
# Note: You forgot to capitalize the 'A' in the original
class AbstractBaseClass(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def load(self, input):
"""Retrieve data from the input source and return an object."""
return
concrete.py
from base import AbstractBaseClass
class ConcreteChildClass(AbstractBaseClass):
def load(self, input):
return input.read()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With