Subclass on the other map

I have created:

List item

a program on the map ‘X’
a class file on the map ‘Y’
its subclass file on the map ‘Z’

The program uses the function ‘importlib.import_module’ for the class and its subclass. Unfortunately the subclass cannot be allocated because the classname is not defined!? What should I have done wrong?

  • A “class file” is not a thing in Python. I assume you created a source with a class in it. That is called a module. When you import a module it adds the module to the name space. That means that e.g. classes in the module can be used by specifying <module name>.<class name>. Modules in Python can (and should) contain more than one class, because the module is just another to group things into a namespace.

  • So if in your program you want to import the subclass you import the module the subclass is in. That module does not contain the definition of your class. That is where the error is coming from. To get the class to inherit from you need to import the class to inherit from in the module of the subclass.

You can import modules (and also packages which you should read about) in an easier way than with importlib.import_module. Look at the import statement, which enables things like writing from a_module import a_class and several other goodies to control how importing changes your namespace.

Without seeing your code, and the actual exception and error message you
are getting, we would be guessing.

My guess is that you have a typo in the subclass name, or you are not
using the fully qualified name, or you are trying to use the subclass
before you have defined it.

Why are you using importlib.import_module instead of import?

It might help if you read these:

and remember to post your code and the exception as text, not as
screenshots or images. You can format the code using the </> button in
the fancy text editor widget, or if replying by email you can surround
it by three backticks:

backticks must be on a line of their own
code goes here