6 ms·
I do use them quite often without much issue, not sure why some people are struggling with it. Say you have a main package "mylib" with a subpackage "mylib.uti
by Galanwe 5y ago
I do use them quite often without much issue, not sure why some people are struggling with it.
Say you have a main package "mylib" with a subpackage "mylib.utils". Typically I like to see "mylib.utils" imports as being in one of (roughly) 4 categories:
- standard imports, that I would put first in the file (e.g. "import logging")
- external imports, that I would put second in the file (e.g. "import requests")
- library local imports, bits and pieces from the "mylib" package that you want to reuse in "mylib.utils", but are external to the current package (e.g. "from mylib.email import client")
- and package local imports, which I see as implementation details of the current subpackage, and should be agnostic from the overall architecture of "mylib" (e.g. "from .helpers import help_function")
The last category are modules that only make sense from within "mylib.utils", should relocate with it even if it is renamed or moved elsewhere, and shouldn't require a change whatever the structure of "mylib" becomes, which is why I would use "mylib.utils" relative imports in there.