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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4631377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:38:05  来源:igfitidea点击:

Unresolved Import Issues with PyDev and Eclipse

pythonpydevpython-import

提问by ely

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.

我对 PyDev 和 Python 非常陌生,尽管我已经大量使用 Eclipse for Java。我正在尝试完成一些 Dive Into Python 示例,这感觉像是一个极其微不足道的问题,只是变得非常烦人。我正在使用 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

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

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

这是我在 PyDev/Eclipse 项目中处理的 example.py 文件:

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__.pyfiles to just about every directory possible and it doesn't help anything. I've tried adding __init__.pyfiles 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__.pyfiles to all of the directories in between simultaneously. Neither works.

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

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/

我想要做的就是在其他目录的某个地方有一个项目,比如说/Desktop/MyStuff/Project,我有 example.py ... 然后从 example.py 我想导入 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.

我能找到的每个留言板响应都只是说使用该sys.path.append()函数将此目录添加到我的路径中,然后将其导入……但这正是我在代码中所做的,但它不起作用。

I have also tried the Ctrl-1trick 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__.pyfiles has missed.

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

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

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

采纳答案by David German

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.

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

回答by Paul

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.

我刚刚将一个 WXWindows 项目升级到 Python 2.7,并且让 Pydev 识别新的解释器没有尽头。做与上面配置解释器相同的事情,重新安装 Eclipse 和 Pydev。认为 python 的某些部分一定已经损坏,所以我重新安装了所有东西。啊!关闭并重新打开项目,并在所有这些更改之间重新启动 Eclipse。

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!

最后注意到您可以通过右键单击项目来“删除 PyDev 项目配置”。然后又可以做成PyDev项目了,现在金子般的好!

回答by Martin

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.

我修复了我的 pythonpath 并且当我通过控制台导入东西时一切都很好,但是所有这些以前未解析的导入仍然在我的代码中被标记为错误,无论我重新启动 eclipse 或刷新/清理项目多少次。

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.

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

回答by PachinSV

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"
  • 从“Python 解释器”中删除 Python 解释器并重新添加。
  • 或者只是在项目中使用的解释器中添加带有库的文件夹,在我的情况下,我使用的是“bottle”,我添加的文件夹是“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".

现在我看不到错误了,代码完成功能也适用于“瓶子”。

回答by MutantXenu

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:

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

Pydev and *.pyc Files

Pydev 和 *.pyc 文件

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

... PyDev 和 pyc 文件存在问题。在我尝试引用的特定库的情况下,所提供的只是 pyc 文件。

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
  1. https://github.com/Mysterie/uncompyle2安装 uncompyle2
  2. 针对站点包库中的 *​​.pyc 文件运行 uncompyle2。例子:

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

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

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

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

回答by zhaokongsheng

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

project-->properties-->pydev-pythonpath-->external libraries-->添加源文件夹,添加项目的PARENT FOLDER。然后重启eclipse。

回答by Brana

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

这是对我有用的(由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.

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

回答by Ridwan Kurniawan

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
  1. init.py添加到您的“~/Desktop/Python_Tutorials/diveintopython/py”文件夹
  2. 转到 Window --> Preferences --> PyDev --> Interpreters --> Python Interpreter 以删除您的 Python Interpreter 设置(原因是因为 PyDev 无法自动刷新对任何系统 PythonPath 所做的任何更新)
  3. 添加具有与以前相同的详细信息的解释器(这将使用对 PythonPath 进行的更新来刷新您的 Python 解释器设置)
  4. 最后,由于您的“~/Desktop/Python_Tutorials/diveintopython/py”文件夹不是标准的 PythonPath,因此您需要将其添加进去。有两种方法可以做到

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

一种。根据 David German 的建议。但是,这仅适用于您在 b 中的特定项目。将“~/Desktop/Python_Tutorials/diveintopython/py”添加到Window下的新PythonPath --> Preferences --> PyDev --> Interpreters --> Python Interpreter --> Libraries subtab --> NewFolder

Hope it helps.

希望能帮助到你。

回答by BitByte_Bake

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:

我正在使用 eclipse kepler 4.3、PyDev 3.9.2,在我的 ubuntu 14.04 上我遇到了同样的问题。我尝试并花了几个小时,上面所有的选项都是徒劳的。然后我尝试了以下很棒的方法:

  • Select Project-> RightClick-> PyDev-> Remove PyDev Project Config
  • file-> restart
  • 选择Project-> RightClick-> PyDev-> Remove PyDev Project Config
  • 文件->重新启动

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

我使用 Python 2.7 作为解释器,虽然我认为它没有影响。

回答by Java By Kiran

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