PyDev和Eclipse的未解决的导入问题

问题:PyDev和Eclipse的未解决的导入问题

我对PyDev和Python还是很陌生,尽管我已经大量使用Eclipse来编写Java。我正在尝试研究一些“ Dive Into Python”示例,这感觉像一个极其琐碎的问题,变得非常烦人。我正在使用Ubuntu Linux 10.04。

我希望能够使用odbchelper.py文件,该文件位于目录中 /Desktop/Python_Tutorials/diveintopython/py

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

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

这可以正常工作,但是然后我希望代码的下一行是:

import odbchelper

并且这每次都会导致无法解决的导入错误。我已经将__init__.py文件添加到几乎所有可能的目录中,但它无济于事。我尝试一次将__init__.py文件一次添加到项目位置和odbchelper.py文件之间的各个目录级别,并且还尝试同时将__init__.py文件添加到介于两者之间的所有目录中。都不行。

我要做的就是在其他目录中的某个地方有一个项目,例如/Desktop/MyStuff/Project,其中有example.py …,然后从example.py中导入,我想从中导入odbchelper.py/Desktop/Python_Tutorials/diveintopython/py/

我可以找到每个留言板响应,只是说要使用该sys.path.append()功能将该目录添加到我的路径,然后将其导入…,但这恰恰是我在代码中所做的,并且不起作用。

我也尝试了Ctrl1技巧来抑制错误消息,但该程序仍无法正常运行。我收到一个错误ImportError: No module named odbchelper。因此,显然没有添加路径,或者存在一些我添加__init__.py文件的所有排列都遗漏的问题。

如此简单的事情令人非常沮丧……从我的机器上其他地方存在的文件中调用事物……需要这么多的努力。

I am very new to PyDev and Python, though I have used Eclipse for Java plenty. I am trying to work through some of the Dive Into Python examples and this feels like an extremely trivial problem that’s just becoming exceedingly annoying. I am using Ubuntu Linux 10.04.

I want to be able to use the file odbchelper.py, which is located in the directory /Desktop/Python_Tutorials/diveintopython/py

Here is my example.py file that I’m working on in my PyDev/Eclipse project:

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

This works fine, but then I want the next line of my code to be:

import odbchelper

and this causes an unresolved import error every time. I have added __init__.py files to just about every directory possible and it doesn’t help anything. I’ve tried adding __init__.py files one at a time to the various levels of directories between the project location and the odbchelper.py file, and I’ve also tried adding the __init__.py files to all of the directories in between simultaneously. Neither works.

All I want to do is have a project somewhere in some other directory, say /Desktop/MyStuff/Project, in which I have example.py … and then from example.py I want to import odbchelper.py from /Desktop/Python_Tutorials/diveintopython/py/

Every message board response I can find just saying to use the sys.path.append() function to add this directory to my path, and then import it … but that is precisely what I am doing in my code and it’s not working.

I have also tried the Ctrl1 trick to suppress the error message, but the program is still not functioning correctly. I get an error, ImportError: No module named odbchelper. So it’s clearly not getting the path added, or there is some problem that all of my many permutations of adding __init__.py files has missed.

It’s very frustrating that something this simple… calling things from some file that exists somewhere else on my machine… requires this much effort.


回答 0

在pydev项目的属性中,有一个名为“ PyDev-PYTHONPATH”的窗格,以及一个名为“ External Libraries”的子窗格。您可以__init__.py使用该窗格将源文件夹(任何带有的文件夹)添加到路径。然后,您的项目代码将能够从那些源文件夹中导入模块。

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.


回答 1

我正在使用eclipse kepler 4.3,PyDev 3.9.2和在Ubuntu 14.04上遇到相同的问题。我尝试并花费了数小时,使用了以上所有大多数选项,但徒劳无功。然后我尝试了以下很棒的方法:

  • 选择项目 – > RightClick-> 的PyDev – > 删除PyDev的项目配置
  • 文件-> 重新启动

我认为我使用Python 2.7作为解释器,尽管它没有作用。

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.


回答 2

我刚刚将WXWindows项目升级到Python 2.7,并且让Pydev识别新的解释器没有麻烦。进行与上述配置解释器相同的操作,重新安装了Eclipse和Pydev。以为python的某些部分一定已损坏,所以我再次重新安装了所有内容。啊!关闭并重新打开项目,然后在所有这些更改之间重新启动Eclipse。

最终注意到,您可以通过右键单击项目来“删除PyDev项目配置”。然后可以将其重新制作为PyDev项目,现在它就像金子一样好!

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!


回答 3

我修复了pythonpath,当我通过控制台导入内容时,一切都变得很花哨,但是无论我重新启动eclipse或刷新/清理项目多少次,所有这些以前无法解决的导入仍被标记为我的代码中的错误。

我右键单击项目-> Pydev->删除错误标记,它摆脱了这个问题。不用担心,如果您的代码中包含实际错误,它们将被重新标记。

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.


回答 4

项目->属性-> pydev-pythonpath->外部库->添加源文件夹,添加项目的父文件夹。然后重新启动Eclipse。

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


回答 5

这是为我工作的(由soulBit推荐):

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

这是有史以来最简单的解决方案,它完全消除了烦人的事情。

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.


回答 6

有两种方法可以解决此问题:

  • 从“ Python解释器”中删除Python解释器,然后再次添加。
  • 或者只是在项目中使用的解释器中添加包含库的文件夹,就我而言,我使用的是“ bottle”,添加的文件夹是“ c:\ Python33 \ Lib \ site-packages \ bottle-0.11.6 -py3.3.egg”

现在,我不再看到该错误,并且代码完成功能也可以与“瓶”一起使用。

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”.


回答 7

我正在运行Eclipse 4.2.0(Juno)和PyDev 2.8.1,并在将lib安装到我的站点软件包路径时遇到了此问题。根据这个SO问题:

Pydev和* .pyc文件

… PyDev和pyc文件存在问题。对于我尝试引用的特定库,仅提供pyc文件。

这是我为解决此问题所做的工作:

  1. https://github.com/Mysterie/uncompyle2安装uncompyle2
  2. 对site-packages库中的* .pyc文件运行uncompyle2。例:

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

  3. 将由uncompyle2生成的结果* .pyc_dis文件重命名为* .py
  4. 将这些* .py文件移动/复制到站点软件包路径
  5. 在Eclipse中,选择File> Restart

与.pyc文件有关的未解决的导入错误现在应该消失了。

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.


回答 8

以下,我认为会解决问题

  1. init .py 添加到“〜/ Desktop / Python_Tutorials / diveintopython / py”文件夹中
  2. 转到窗口->首选项-> PyDev->解释器-> Python解释器以删除您的Python解释器设置(原因是因为PyDev无法自动刷新对任何系统PythonPath所做的任何更新)
  3. 使用与之前相同的详细信息添加解释器(这将通过对PythonPath进行的更新来刷新Python解释器设置)
  4. 最后,由于您的“〜/ Desktop / Python_Tutorials / diveintopython / py”文件夹不是标准的PythonPath,因此需要将其添加。有两种方法可以实现

一个。按照大卫·德文的建议。但是,这仅适用于您所在的特定项目。在“窗口”->首选项-> PyDev->解释器-> Python解释器->库子选项卡-> NewFolder下,将“〜/ Desktop / Python_Tutorials / diveintopython / py”添加到新的PythonPath中

希望能帮助到你。

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.


回答 9

在尝试解决问题之后,通过理解PYTHONPATH,解释器和语法,我在导入其他库时遇到了一些问题,我发现我做了所有的编写,但问题仍然存在。之后,我只在出现导入错误的文件中添加一个新的空行,然后保存它们并解决了错误

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.


回答 10

KD.py

class A:
a=10;

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

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

这对我来说很完美

另一个例子是

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