简体   繁体   English

PyDev:未解决的导入

[英]PyDev: Unresolved import

I use PyDev in Eclipse and have a custom source path for my Python project: src/main/python /. 我在Eclipse中使用PyDev,并为我的Python项目提供了一个自定义源路径: src / main / python /。 The path is added to the PythonPath. 路径已添加到PythonPath。

Now, i want to use the library pyMIR: https://github.com/jsawruk/pymir , which doesn't has any install script. 现在,我想使用pyMIR库: https : //github.com/jsawruk/pymir ,它没有任何安装脚本。 So I downloaded it and and included it direclty into my project as a Pydev package, the complete path to the pyMIR is: src/main/python/music/pymir . 所以我下载了它,并将其作为Pydev软件包包含在我的项目中,pyMIR的完整路径是: src / main / python / music / pymir

In the music package ( src/main/python/music ), now i want to use the library and import it via: from pymir import AudioFile . 在音乐包( src / main / python / music )中,现在我想使用该库并通过以下from pymir import AudioFilefrom pymir import AudioFile There appears no error, so class AudioFile is found. 没有出现错误,因此找到了AudioFile类。

Afterward, I want to read an audio file via: AudioFile.open(path) and there i get the error "Undefined variable from import: open". 之后,我想通过以下方式读取音频文件: AudioFile.open(path)然后出现错误“来自导入的未定义变量:打开”。 But when I run the script, it works, no error occurs. 但是,当我运行脚本时,它可以工作,不会发生任何错误。

Furthermore, when I look in the pyMIR package, there are also unresolved import errors. 此外,当我查看pyMIR软件包时,还存在未解决的导入错误。 For example: from pymir import Frame in the class AudioFile produces the error: "Unresolved import: Frame", when I change it to from music.pymir import Frame , the error disappears, but then I get an error when it runs: "type object 'Frame' has no attribute 'Frame'". 例如: from pymir import Frame ,类AudioFile中的from pymir import Frame产生错误:“ Unresolved import:Frame”,当我将其更改为from music.pymir import Frame ,该错误消失,但是当它运行时我得到一个错误:“ type对象“框架”没有属性“框架””。

  1. What I have to change, another import or how to include a Pydev package? 我必须更改什么,再次导入或如何包含Pydev软件包?

  2. When I make a new project with a standard path "src", then no "unresolved impor" errors appear. 当我使用标准路径“ src”创建一个新项目时,则不会出现“未解决的重要问题”错误。 Where is the difference to src/main/python ? src / main / python的区别在哪里? Because I changed the path of source folders to src/main/python . 因为我将源文件夹的路径更改为src / main / python

Thanks in advance. 提前致谢。

I tried to download and install the pymir package. 我试图下载并安装pymir软件包。 There is one project structure that works for me: 有一个适合我的项目结构:

project/music/
project/music/pymir/
project/music/pymir/AudioFile
project/music/pymir/...
project/music/audio_files/01.wav
project/music/test.py

The test.py: test.py:

import numpy
from pymir import AudioFile
filename = "audio_files/01.wav"
print "Opening File: " + filename
audiofile = AudioFile.open(filename)
frames = audiofile.frames(2048, numpy.hamming)
print len(frames)

If I moved 'test.py' out from 'music' package, I haven't found a way to make it work. 如果我将“ test.py”从“音乐”包中移出,我还没有找到一种使之工作的方法。 The reason why the project structure is sensitive and tricky is, in my opinion, the pymir package is not well structured. 在我看来,项目结构之所以敏感且棘手,是因为pymir软件包的结构不够好。 Eg, the author set module name as "Frame.py" and inside the module a class is named "Frame". 例如,作者将模块名称设置为“ Frame.py”,并且在模块内部将一个类命名为“ Frame”。 Then in "__init__.py", codes are like "from Frame import Frame". 然后在“ __init__.py”中,代码​​类似于“从框架导入框架”。 And in "AudioFile.py", codes are "from pymir import Frame". 在“ AudioFile.py”中,代码​​是“来自pymir import Frame”。 I really think the naming and structure of the current pymir is messy. 我真的认为当前pymir的命名和结构很混乱。 Suggest you use this package carefully 建议您谨慎使用此软件包

在基本文件夹位置添加“ __init__.py”空文件,即可正常工作

  1. unzip the folder pymir to site-packages , make sure the path like pymir文件夹解压缩到site-packages ,确保路径类似于

     site-packages\\pymir site-packages\\pymir\\AudioFile.py site-packages\\pymir\\Frame.py site-packages\\pymir\\... 
  2. comment the content of the file __init__.py 注释文件__init__.py的内容

     #from AudioFile import AudioFile #from Frame import Frame #from Spectrum import Spectrum 
  3. test it 测试一下

     import numpy as np import matplotlib.pyplot as plt from pymir.AudioFile import AudioFile filename = '../wavs/cxy_6s_mono_16KHz.wav' audiofile = AudioFile.open(filename) plt.plot(audiofile) plt.show() frames = audiofile.frames(2048, np.hamming) print(len(frames)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM