0

I have the following directory structure in python.

├── mel2samp.py
├── tacotron2
│   ├── layers.py

In mel2samp.py I want to import TacotronSTFT from tacatron2.layers using these lines of code

import sys
sys.path.insert(0, 'tacotron2')
from tacotron2.layers import TacotronSTFT

But it throws an error ImportError: No module named tacotron2.layers.

Samuel Mideksa
  • 423
  • 8
  • 19

3 Answers3

1

Also need an empty __init__.py file in tacotron2 folder. After that you can do:

import sys
from tacotron2.layers import TacotronSTFT
denis_lor
  • 6,212
  • 4
  • 31
  • 55
1
import sys
sys.path.insert(0, 'tacotron2')
from tacotron2.layers import TacotronSTFT
# Use TacotronSTFT

But it is recommended to make tacotron2 as a package by adding init.py

Then you can use it as

from tacotron2.layers import TacotronSTFT #Use TacotronSTFT

Rupesh Goud
  • 131
  • 1
  • 9
1

You can make your folder a package by adding __init__.py
You can read more about it here

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later (deeper) on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Morty C-137
  • 111
  • 10