0

I am trying to follow along with a project written by Mike Smales - "Sound Classification using Deep Learning". In there, the author wrote a helper file called wavfilehelper.py:

wavehelper.py Code

import struct

class WavFileHelper():
    
    def read_file_properties(self, filename):

        wave_file = open(filename,"rb")
        
        riff = wave_file.read(12)
        fmt = wave_file.read(36)
        
        num_channels_string = fmt[10:12]
        num_channels = struct.unpack('<H', num_channels_string)[0]

        sample_rate_string = fmt[12:16]
        sample_rate = struct.unpack("<I",sample_rate_string)[0]
        
        bit_depth_string = fmt[22:24]
        bit_depth = struct.unpack("<H",bit_depth_string)[0]

        return (num_channels, sample_rate, bit_depth)

In his main program he calls the helper file like this:

from helpers.wavfilehelper import WavFileHelper

wavfilehelper = WavFileHelper()

However, when I run this block of code in PyCharm, it complains "ModuleNotFoundError: No module named 'helpers.wavfilehelper'"...how can I get this helper file to work in the PyCharm environment? Do I have to put the wavehelper.py file in a special folder to be called?

Any help will be greatly appreciated!

Joe
  • 357
  • 2
  • 10
  • 32

1 Answers1

1

It is important to look at (and quote in your question) the actual error messages! In this case, which line is in-error? It is not the instantiation line, but the import - Python is unable to find the module on your machine (using its system paths).

Earlier in the article, the author talks about downloading his files from GitHub (to your machine). Did you follow that step?

Web.Ref: further information about solving this error

d-n
  • 26
  • 3
  • Thanks for your input. I did download the helper file and currently have the wavfilehelper.py in a folder name helpers. Is there a way to point to helper file correctly? As `helpers.wavefilehelper` did not work for me. – Joe Nov 08 '20 at 06:28
  • 1
    Yes there is. please read about the sys.path at https://docs.python.org/3/library/sys.html and then https://stackoverflow.com/questions/16114391/adding-directory-to-sys-path-pythonpath – d-n Nov 09 '20 at 07:51
  • 1
    However, Python expects to find import-modules either in the current directory or in some sub-directory thereof. So, either move the contents of your helpers folder into the project folder, or move the folder to become a sub-folder of the project folder (in which case, prepend the import's module name - directory_name.helpers). Watch out though: you're using the name "helpers" to mean several different components! – d-n Nov 09 '20 at 08:00