如何在 PyCharm 中使用帮助文件

How to use helper files in PyCharm

我正在尝试跟随 project written by Mike Smales - "Sound Classification using Deep Learning"。在那里,作者写了一个名为 wavfilehelper.py:

的帮助文件

wavehelper.py代码

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)

在他的主程序中,他这样调用辅助文件:

from helpers.wavfilehelper import WavFileHelper

wavfilehelper = WavFileHelper()

然而,当我 运行 PyCharm 中的这个代码块时,它会抱怨“ModuleNotFoundError: No module named 'helpers.wavfilehelper'”...我怎样才能得到这个帮助文件到在 PyCharm 环境中工作?是不是要把wavehelper.py文件放在特殊的文件夹里才能调用?

任何帮助将不胜感激!

是的,为了 运行 指定的 python main.py 程序,它想通过 [= 从 helpers/wavehelper.py 导入一个名为 WavFileHelper 的 class 13=]

查看(并在您的问题中引用)实际的错误消息很重要!在这种情况下,哪一行是in-error?它不是实例化行,而是导入 - Python 无法在您的机器上找到模块(使用其系统路径)。

在本文前面,作者谈到了从 GitHub 下载他的文件(到您的机器)。你按照那个步骤做了吗?

Web.Ref: further information about solving this error