How to avoid long path to class name in the __module__ property?

Viewed 24

I am writing python library with the following structure:

src
  mylib
    __init__.py
    mylib.py
    utils.py

I have a class MyClass defined in the mylib.py. I want to be able to import it by the user as from mylib import MyClass. Inside __init__.py I put from .mylib import MyClass.

Everything works but the import location look ugly (it is probably taken from MyClass.__module__):

from mylib import MyClass
print(MyClass)  # <class 'mylib.mylib.MyClass'>

Why do I have duplication mylib.mylib in the output and how to avoid it? My dumb solution is to put all my library code into __init__.py but it feels there should be a better solution.

For example, when you do the same with PyTorch library

from torch import Tensor
print(Tensor)  # <class 'torch.Tensor'>

There is no such duplication, even though I would expect <class 'torch._tensor.Tensor'> since it is imported from file _tensor.py.

0 Answers
Related