Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import something from a nested child directory with Python?

Tags:

python

In my main file (root level), I have:

from deepspeech2_utils import reduce_tensor, check_loss

And I also have an __init__.py that has:

from submodules.deepspeech2 import utils as deepspeech2_utils

I have a directory structure that looks like:

main.py
__init__.py
-submodules
  -deepspeech2
    -utils.py

But I get an error:

    from deepspeech2_utils import reduce_tensor, check_loss
ImportError: No module named deepspeech2_utils

I also tried:

from submodules.deepspeech2.utils import reduce_tensor, check_loss

but get the same error.

What am I doing wrong?

like image 327
Shamoon Avatar asked Apr 05 '20 15:04

Shamoon


People also ask

Can you import from parent directory Python?

From Python 3.3, referencing or importing a module in the parent directory is not allowed, From the below example you can clearly understand this.

How do I import a file from one directory to another in Python?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.

How do I import a package from one package to another in Python?

To access any module or file from a Python package, use an import statement in the Python source file where you want to access it. Import of modules from Python Packages is done using the dot operator (.). Importing modules and packages helps us make use of existing functions and code that can speed up our work.

What does __ init __ py do in Python?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.


1 Answers

So the way directories are recognized as modules in python is by the presence of an __init__.py file within those directories. These __init__.py files don't need to have code. So change your directory structure to look like this

root
    main.py
    __init__.py
    submodules
        __init__.py
        deepspeech2
            __init__.py
            utils.py

Now once this is done, your import statement (which didn't work without the above directory changes) in __init__.py has a scope, it will not be visible within your main.py - that has a different scope. In order to achieve what you are doing change your import statement in main.py to

from root.deepspeech2_utils import reduce_tensor, check_loss

I have to advise apart from namespacing reasons, importing in __init__.py is not encouraged, since users of your module may just want to import specific things from your module/submodules and your import statements in __init__.py will force them to have more imports than they want. Here is an answer from another post that talks about such concerns in detail

like image 157
Sam Thomas Avatar answered Sep 19 '22 11:09

Sam Thomas