I am creating a package in python, that has some optional dependencies. These dependencies are not required for core features but their lack will disable use of some class (it is an orm which offers a field for storing html. lxml is used to store and retrieve the value).
Obviously I may just not create the class if the dependency cannot be imported. But this could be confusing for the user, as he gets ‘module x has no attribute y’ which is not the real problem.
So is there any way to issue the meaningful error message if someone wants to import or use a class (I was also thinking about creating a class with __getattribute__ raising error). Is there any recommended way to do it? Some interesting way it was done in some high-quality package?
Not really. But if the optional import fails then you could provide “stub” classes which would explain the error if instantiated.