PyDev 和 Eclipse 未解决的导入问题
尽管我已经多次使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在 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.我正在使用 eclipse kepler 4.3、PyDev 3.9.2,在我的 ubuntu 14.04 上遇到了同样的问题。我尝试并花了几个小时,使用了上述大多数选项,但徒劳无功。然后我尝试了以下很棒的操作:
我使用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:
And I was using Python 2.7 as an interpreter, although it doesn’t effect, I think.
我刚刚将 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!
我修复了我的 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.
项目-->属性-->pydev-pythonpath-->外部库-->添加源文件夹,添加项目的父文件夹。然后重新启动eclipse。
project-->properties-->pydev-pythonpath-->external libraries --> add source folder, add the PARENT FOLDER of the project. Then restart eclipse.
这是对我有用的方法(由 SoulBit 建议):
这是有史以来最简单的解决方案,它完全消除了烦人的事情。
Here is what worked for me (sugested by soulBit):
This is the simplest solution ever and it completely removes the annoying thing.
有两种方法可以解决这个问题:
现在我不再看到错误了,代码完成功能也可以与“bottle”一起使用。
There are two ways of solving this issue:
Now I don't see the error anymore, and the code completion feature works as well with "bottle".
我正在运行 Eclipse 4.2.0 (Juno) 和 PyDev 2.8.1,并且在将 lib 安装到我的站点包路径时遇到了这个问题。根据这个问题:
Pydev 和 *.pyc 文件
...存在问题PyDev 和 pyc 文件。对于我尝试引用的特定库,所有提供的都是 pyc 文件。
这是我解决这个问题的方法:
针对 site-packages 库中的 *.pyc 文件运行 uncompyle2。示例:
uncompyle2 -r -o /tmp /path/to/site-packages/lib
与 .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:
Run uncompyle2 against the *.pyc files in the site-packages lib. Example:
uncompyle2 -r -o /tmp /path/to/site-packages/lib
The unresolved import error relating to .pyc files should now disappear.
在我看来,以下内容将解决问题
一点。按照大卫·日耳曼的建议。然而,这仅适用于您所在的特定项目
b.将“~/Desktop/Python_Tutorials/diveintopython/py”添加到Window下的新PythonPath中 -->偏好设置 --> PyDev -->口译员 --> Python 解释器 -->库子选项卡 -->新文件夹
希望有帮助。
Following, in my opinion will solve the problem
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.
其他答案都不适合我。我可以在导入的文件中看到该函数,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.
这对我来说完美文件
另一个例子是
THIS works perfectly file for me
Another example is