PyDev: Unresolved import

浪尽此生 提交于 2020-01-03 05:27:05

问题


I use PyDev in Eclipse and have a custom source path for my Python project: src/main/python/. The path is added to the PythonPath.

Now, i want to use the library pyMIR: https://github.com/jsawruk/pymir, which doesn't has any install script. 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.

In the music package (src/main/python/music), now i want to use the library and import it via: from pymir import AudioFile. There appears no error, so class AudioFile is found.

Afterward, I want to read an audio file via: AudioFile.open(path) and there i get the error "Undefined variable from import: open". 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. 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'".

  1. What I have to change, another import or how to include a Pydev package?

  2. When I make a new project with a standard path "src", then no "unresolved impor" errors appear. Where is the difference to src/main/python? Because I changed the path of source folders to src/main/python.

Thanks in advance.


回答1:


I tried to download and install the pymir package. 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:

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. The reason why the project structure is sensitive and tricky is, in my opinion, the pymir package is not well structured. E.g., the author set module name as "Frame.py" and inside the module a class is named "Frame". Then in "__init__.py", codes are like "from Frame import Frame". And in "AudioFile.py", codes are "from pymir import Frame". I really think the naming and structure of the current pymir is messy. Suggest you use this package carefully




回答2:


add "__init__.py" empty file in base folder location and it works




回答3:


  1. unzip the folder pymir to site-packages, make sure the path like

    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

    #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))
    


来源:https://stackoverflow.com/questions/23237692/pydev-unresolved-import

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