PyDev 和 Eclipse 未解决的导入问题

发布于 2024-10-10 15:00:03 字数 1179 浏览 7 评论 0原文

尽管我已经多次使用 Eclipse for Java,但我对 PyDev 和 Python 还很陌生。我正在尝试解决一些 Dive Into Python 示例,这感觉像是一个极其微不足道的问题,但现在却变得非常烦人。我使用的是 Ubuntu Linux 10.04。

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

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

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() 函数来添加此内容目录到我的路径,然后导入它......但这正是我在代码中所做的,但它不起作用。

我还尝试了 Ctrl-1 技巧来抑制错误消息,但程序仍然无法正常运行。我收到错误:ImportError:没有名为 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 Ctrl-1 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

蓝咒 2024-10-17 15:00:03

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

柠檬色的秋千 2024-10-17 15:00:03

我正在使用 eclipse kepler 4.3、PyDev 3.9.2,在我的 ubuntu 14.04 上遇到了同样的问题。我尝试并花了几个小时,使用了上述大多数选项,但徒劳无功。然后我尝试了以下很棒的操作:

  • 选择项目 - >右键单击-> 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.

删除会话 2024-10-17 15:00:03

我刚刚将 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!

著墨染雨君画夕 2024-10-17 15:00:03

我修复了我的 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.

素衣风尘叹 2024-10-17 15:00:03

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

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

口干舌燥 2024-10-17 15:00:03

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

骄兵必败 2024-10-17 15:00:03

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

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

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

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

清泪尽 2024-10-17 15:00:03

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

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 文件移动/复制到 site-packages 路径
  5. 在 Eclipse 中,选择文件>重新启动

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

归属感 2024-10-17 15:00:03

在我看来,以下内容将解决问题

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

一点。按照大卫·日耳曼的建议。然而,这仅适用于您所在的特定项目
b.将“~/Desktop/Python_Tutorials/diveintopython/py”添加到Window下的新PythonPath中 -->偏好设置 --> PyDev -->口译员 --> Python 解释器 -->库子选项卡 -->新文件夹

希望有帮助。

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.

在尝试解决问题后,我在导入其他库时遇到了一些问题,通过理解
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.

一枫情书 2024-10-17 15:00:03

其他答案都不适合我。我可以在导入的文件中看到该函数,pylint 可以找到它,并且所有测试都通过了(是的,该函数已被执行)。直到我碰巧在 site.packages 目录上运行 ls 并看到 module.py 文件和 module.pyi 文件时,一切都没有帮助。 pyi 文件是空的,我不知道它从哪里来(我确信我手指粗了一些东西,但不知道是什么),但我确实知道,如果你有一个它可以'不要是空的。

我删除了该文件,重新启动 Eclipse,一切都恢复正常。

None of the other answers worked for me. I could see the function in the imported file, pylint could find it, and all tests passed (and yes, the function was exercised). Nothing helped until I happened to run ls on the site.packages directory and saw both the module.py file AND a module.pyi file. The pyi file was empty, and I have no idea where it came from (I'm sure I fat-fingered something but no idea what), but I do know that if you have one it can't be empty.

I removed the file, restarted Eclipse and everything was back to normal.

念三年u 2024-10-17 15:00:03
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")    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文