pythonimportmodulelogic

import logic in python - for some modules we follow the structure from A.B.C import D, but for some modules we can bypass that and do from A import E


i'm having trouble figuring out the importing logic in python, i'll use an existing example from the langchain library

for module name langchain.chains.retrieval_qa.base.VectorDBQA a direct import works, i.e.

from langchain import VectorDBQA

but for another module name langchain.document_loaders.epub.UnstructuredEPubLoader a direct import won't work

from langchain import UnstructuredEPubLoader

will give me an error

I need to do

from langchain.document_loaders import UnstructuredEPubLoader

I do not understand the logic behind this? How am I to know from the reading the documentation, which libraries can be imported directly and which ones need the structure to be followed?


Solution

  • The logic of whether you can import something directly from the main package (from package import module) or if you need to specify a subpackage (from package.subpackage import module) is entirely dependent on how the library's internal structure is set up, which is defined in its __init__.py files within each directory.

    If a module can be imported directly from the main package, it is usually because the library's __init__.py file in the main package has a specific import statement for that module, making it available at that level. If it's not imported in the __init__.py file, you'll need to specify the full path to where the module is contained within the package.

    The import statements are usually specified in the documentation of the library, else you will have to search through its file structure.

    You can also print(dir(langchain)) to have a full overview of the package and help(langchain) to get information about it.

    Hope that can help