"There is no module named abc_base"

I am trying to implement an abstract class in python (actually in a django application) and I am running 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 knocked down my problem with 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 information

 >>> import sys >>> print sys.version 2.7.3 (default, Jan 2 2013, 13:56:14) [GCC 4.7.2] 

I am new to python (as this question may make it painfully obvious), but I cannot figure out how to find "abc", but not "abc_base". My reading and google search did not give me answers to this question. Thanks in advance and apologize if this is a stupid question.

+4
source share
1 answer

I assume you follow this tutorial?

The mistake you made (and, to be honest, the manual is unclear), it is assumed that abc_base is the name of some module that lives inside the standard library.

Most likely, this is just the name of the very first python file in the tutorial that defines the PluginBase class.


To adapt the code to work for you, you need to import from any file that contains the desired base class, and not from abc_base .

Note. Since the class names and file names in your example are identical, I went ahead and changed the file names to make it more clear what is 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() 
+14
source

Source: https://habr.com/ru/post/1502841/


All Articles