Eclipse + PyDev ImportError

2019-08-02 14:25发布

问题:

I am having trouble getting PyDev on Eclipse to recognize installed modules (gensim), which work fine in IDLE. I am using Windows Vista, 32-bit. Python 2.7.

I have found this question asked: here, here, here, and here.

The recommended solution is to go to preferences > pydev > interpreter - python, and remove and re-add (w/ Auto Config) the python interpreter. I have done this, and have restarted Eclipse. In PYTHONPATH, C:\Python27\lib\site-packages\gensim-0.8.0-py2.7.egg, appears, but I still run into the import error. My code is:

from gensim import corpora, models, similarities

And this yields:

Traceback (most recent call last):
  File "C:\Users\Jpan\Documents\workspace\FirstProject\src\gensim.py", line 1, in <module>
    from gensim import corpora, models, similarities
  File "C:\Users\Jpan\Documents\workspace\FirstProject\src\gensim.py", line 1, in <module>
    from gensim import corpora, models, similarities
ImportError: cannot import name corpora

Another recommended solution is to manually add the folder by clicking "New Folder" in the bottom part of the interpreter - python screen and navigating to the location where gensim installed. I have also done this, and added C:\Python27\lib\site-packages\gensim-0.8.0-py2.7.egg\gensim, which has all the necessary \__init__.py files. But, I still get the ImportError.

Any suggestions for what else I could try?

回答1:

This is independent of Eclipse/PyDev. You'll get the same error running the code in any other way. Your module imports gensim. The first entry on the PYTHONPATH is the current directory, and your module is called gensim.py, so your module attempts to import iteself. Because imports are cached, you don't get into infinite recursion but get a reference to a module containing... nothing, especially not the things you expected from the "real" gensim module.

The error message should mention this possibility, it's incredibly common. The solution is to rename your file.