PyDev 和 Eclipse 未解决的导入问题

我对 PyDev 和 Python 非常陌生,尽管我已经大量使用过 Eclipse for Java。我正在尝试处理一些 Dive Into Python 示例,这看起来像是一个非常琐碎的问题,而且正变得非常烦人。我正在使用 Ubuntu Linux 10.04。

我希望能够使用目录 /Desktop/Python_Tutorials/diveintopython/py中的 odbchelper.py 文件

下面是我在 PyDev/Eclipse 项目中使用的 example.py 文件:

import sys
sys.path.append("~/Desktop/Python_Tutorials/diveintopython/py")

这样可以很好地工作,但是我希望代码的下一行是:

import odbchelper

这每次都会导致一个未解决的导入错误。我已经将 __init__.py文件添加到几乎所有可能的目录中,但它没有任何帮助。我尝试过在项目位置和 odbchelper.py 文件之间的不同层次的目录中一次添加一个 __init__.py文件,我还尝试过同时在两者之间的所有目录中添加 __init__.py文件。都不管用。

我所要做的就是在其他目录中的某个地方创建一个项目,比如 /Desktop/MyStuff/Project,其中包含 example.py... 然后从 example.py 导入来自 /Desktop/Python_Tutorials/diveintopython/py/的 odbchelper.py

我能找到的每个留言板的回复都是说,使用 sys.path.append()函数将这个目录添加到我的路径中,然后导入它... ... 但这正是我在代码中所做的,而且它没有工作。

我还尝试了 Ctrl-1技巧来抑制错误消息,但是程序仍然没有正常工作。我得到一个错误,ImportError: No module named odbchelper。所以很明显没有添加路径,或者有一些问题,我所有的添加 __init__.py文件的排列都错过了。

非常令人沮丧的是,这么简单的事情... 从我机器上的某个文件中调用东西... 需要这么多的努力。

195900 次浏览

In the properties for your pydev project, there's a pane called "PyDev - PYTHONPATH", with a sub-pane called "External Libraries". You can add source folders (any folder that has an __init__.py) to the path using that pane. Your project code will then be able to import modules from those source folders.

I just upgraded a WXWindows project to Python 2.7 and had no end of trouble getting Pydev to recognize the new interpreter. Did the same thing as above configuring the interpreter, made a fresh install of Eclipse and Pydev. Thought some part of python must have been corrupt, so I re-installed everything again. Arghh! Closed and reopened the project, and restarted Eclipse between all of these changes.

FINALLY noticed you can 'remove the PyDev project config' by right clicking on project. Then it can be made into a PyDev project again, now it is good as gold!

I fixed my pythonpath and everything was dandy when I imported stuff through the console, but all these previously unresolved imports were still marked as errors in my code, no matter how many times I restarted eclipse or refreshed/cleaned the project.

I right clicked the project->Pydev->Remove error markers and it got rid of that problem. Don't worry, if your code contains actual errors they will be re-marked.

There are two ways of solving this issue:

  • Delete the Python interpreter from "Python interpreters" and add it again.
  • Or just add the folder with the libraries in the interpreter you are using in your project, in my case I was using "bottle" and the folder I added was "c:\Python33\Lib\site-packages\bottle-0.11.6-py3.3.egg"

Now I don't see the error anymore, and the code completion feature works as well with "bottle".

I'm running Eclipse 4.2.0 (Juno) and PyDev 2.8.1, and ran into this problem with a lib installed to my site-packages path. According to this SO question:

Pydev and *.pyc Files

...there is an issue with PyDev and pyc files. In the case of the particular lib I tried to reference, all that is delivered is pyc files.

Here's what I did to address this:

  1. Install uncompyle2 from https://github.com/Mysterie/uncompyle2
  2. Run uncompyle2 against the *.pyc files in the site-packages lib. Example:

    uncompyle2 -r -o /tmp /path/to/site-packages/lib

  3. Rename the resulting *.pyc_dis files produced from uncompyle2 to *.py
  4. Move / copy these *.py files to the site-packages path
  5. In Eclipse, select File > Restart

The unresolved import error relating to .pyc files should now disappear.

project-->properties-->pydev-pythonpath-->external libraries --> add source folder, add the PARENT FOLDER of the project. Then restart eclipse.

Here is what worked for me (sugested by soulBit):

1) Restart using restart from the file menu
2) Once it started again, manually close and open it.

This is the simplest solution ever and it completely removes the annoying thing.

Following, in my opinion will solve the problem

  1. Adding the init.py to your "~/Desktop/Python_Tutorials/diveintopython/py" folder
  2. Go to Window --> Preferences --> PyDev --> Interpreters --> Python Interpreter to remove your Python Interpreter setting (reason being is because PyDev unable to auto refresh any updates made to any System PythonPath)
  3. Add in the Interpreter with the same details as before (this will refresh your Python Interpreter setting with updates made to your PythonPath)
  4. Finally since your "~/Desktop/Python_Tutorials/diveintopython/py" folder not a standard PythonPath, you will need to add it in. There are two ways to do it

a. As per what David German suggested. However this only applicable for the particular projects you are in b. Add in "~/Desktop/Python_Tutorials/diveintopython/py" into a new PythonPath under Window --> Preferences --> PyDev --> Interpreters --> Python Interpreter --> Libraries subtab --> NewFolder

Hope it helps.

I am using eclipse kepler 4.3, PyDev 3.9.2 and on my ubuntu 14.04 I encountered with the same problem. I tried and spent hours, with all the above most of the options but in vain. Then I tried the following which was great:

  • Select Project-> RightClick-> PyDev-> Remove PyDev Project Config
  • file-> restart

And I was using Python 2.7 as an interpreter, although it doesn’t effect, I think.

KD.py


class A:
a=10;


KD2.py
from com.jbk.KD import A;
class B:
b=120;


aa=A();
print(aa.a)

THIS works perfectly file for me

Another example is

main.py
=======
from com.jbk.scenarios.objectcreation.settings import _init
from com.jbk.scenarios.objectcreation.subfile import stuff


_init();
stuff();


settings.py
==========
def _init():
print("kiran")




subfile.py
==========
def stuff():
print("asasas")

I had some issues importing additional libraries, after trying to resolve the problem, by understanding PYTHONPATH, Interpreter, and Grammar I found that I did everything write but the problems continue. After that, I just add a new empty line in the files that had the import errors and saved them and the error was resolved.