How to use helper files in PyCharm

蓝咒 提交于 2021-01-29 06:51:08

问题


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!


回答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




回答2:


Yes, in order to run the python main.py program as specified, it wants to import a class named WavFileHelper from helpers/wavehelper.py via from helpers.wavfilehelper import WavFileHelper



来源:https://stackoverflow.com/questions/64710001/how-to-use-helper-files-in-pycharm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!