Python 如何在PyCharm中使用帮助文件
我正试着跟着一个小男孩走。在其中,作者编写了一个名为wavfilehelper.py的助手文件: wavehelper.py代码Python 如何在PyCharm中使用帮助文件,python,pycharm,helper,Python,Pycharm,Helper,我正试着跟着一个小男孩走。在其中,作者编写了一个名为wavfilehelper.py的助手文件: wavehelper.py代码 import struct class WavFileHelper(): def read_file_properties(self, filename): wave_file = open(filename,"rb") riff = wave_file.read(12)
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)
但是,当我在PyCharm中运行这段代码时,它会抱怨“ModuleNotFoundError:没有名为'helpers.wavfilehelper'的模块”…我如何才能让这个助手文件在PyCharm环境中工作?我是否必须将wavehelper.py
文件放入要调用的特殊文件夹中
任何帮助都将不胜感激 是的,为了按照指定运行
python main.py
程序,它想从helpers/wavehelper.py
通过从helpers.WavFileHelper导入WavFileHelper
查看(并在您的问题中引用)实际的错误消息非常重要!在这种情况下,哪一行出错?它不是实例化行,但是import-Python无法在您的机器上找到该模块(使用其系统路径)
在本文前面,作者谈到了从GitHub(到您的机器)下载他的文件。你遵循那一步了吗
谢谢你的回答。我如何让它在IDE中工作?我得到如上所述的“无模块”错误。如何修改
main.py
中的行以使用帮助文件?如何导入wavfilehelper?感谢您的输入。我确实下载了helper文件,目前将wavfilehelper.py放在名为helpers的文件夹中。有没有正确指向帮助文件的方法?作为助手。wavefilehelper
对我不起作用。是的。请阅读sys.path at,然后,Python希望在当前目录或其中的某个子目录中找到导入模块。因此,可以将helpers文件夹的内容移动到项目文件夹中,或者将文件夹移动到项目文件夹的子文件夹中(在这种情况下,请在导入的模块名称-directory_name.helpers之前加上前缀)。不过要注意:您使用的名称“helpers”表示几个不同的组件!
from helpers.wavfilehelper import WavFileHelper
wavfilehelper = WavFileHelper()