在eclipse中将python模块添加到pydev会导致导入错误?
我在 eclipse 上让 PyDev 识别已安装的模块时遇到问题。这是我的详细方法。该机器是Mac(雪豹)。
在终端中,命令
python --version
显示Python 2.6.6。
import unidecode
并且
from unidecode import unidecode
工作都很好!
我在 Eclipse 中安装了 PyDev 并配置了 python 解释器(自动配置)。我选择了所有建议的软件包,然后点击应用和确定。
但是 eclipse 一直抱怨
Traceback (most recent call last):
File "/Users/me/Documents/workspace/myproject/python/pythontest.py", line 12, in <module>
from unidecode import unidecode
ImportError: No module named unidecode
python 文件看起来像这样
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
from unidecode import unidecode
def main():
print unidecode(u"Ågot Aakra")
if __name__ == '__main__':
main()
当我删除脚本中的第一行时,
#!/usr/bin/env python
它会导致相同的错误。
有人知道问题出在哪里吗?
I have a problem getting PyDev on eclipse to recognize already installed modules. Here is my detailed approach. The machine is a Mac (Snow Leopard).
In terminal the command
python --version
shows Python 2.6.6.
import unidecode
and
from unidecode import unidecode
work both fine!
I installed PyDev in Eclipse and went to configured the python interpreter (Auto Config). I selected all the proposed packages and hit Apply and Ok.
But eclipse keeps complaining
Traceback (most recent call last):
File "/Users/me/Documents/workspace/myproject/python/pythontest.py", line 12, in <module>
from unidecode import unidecode
ImportError: No module named unidecode
The python file looks like this
#!/usr/bin/env python
# encoding: utf-8
import sys
import os
from unidecode import unidecode
def main():
print unidecode(u"Ågot Aakra")
if __name__ == '__main__':
main()
When I remove the first line in the script
#!/usr/bin/env python
it results into the same error.
Does someone know where the problem lies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是我的问题的解决方案:
../site-packages/
的路径。 (对我来说,它是/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
for python 2.6 on my Mac with Snoe Leopard.)../site-packages/
。你应该可以走了。 =)
感谢@allparticionts,他们在评论中提供了正确方向的提示。
This is the solution to my problem:
../site-packages/
of your corresponding python version. ( For me it was/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/
for python 2.6 on my Mac with Snoe Leopard.)../site-packages/
of your corresponding python version.And you should be good to go. =)
Thanks @all particionts, who provided hints into the right direction in the comments.
我今天遇到了同样的问题。我正在使用 pydev 并有一个包含许多子包的工作项目。创建新模块后突然我无法在不同的包中使用该模块。令人费解的功能是我可以在同一个子包中使用另一个模块...
最后,在
我使用以下脚本删除了所有已编译的类:
最后我可以做“实际”工作:)
希望它能帮助某人...
I ran into the same problem just today. I am using pydev and had a working project with a number of sub-packages. Suddenly after having created a new module I was not able to use this module in a different package. The puzzling feature was that I could use another module in the same sub-package...
Finally after
I deleted all compiled classes with the following script:
and finally I can do 'actual' work :)
Hope it helps somebody...
您只需将模块添加到 pydev 路径即可。转到项目属性(从上下文菜单)-> PyDev-> Python路径->外部图书馆。根据模块是位于源文件夹还是 zip/egg 文件中,选择“添加源文件夹”或“添加 zip/jar/egg”。导航到 site-packages 目录并指向相关文件或文件夹(我的是:/usr/local/lib/pythonx.x/site-packages)
You can simply add the module to the pydev path. Go to project properties (from the context menu) -> PyDev -> PYTHONPATH -> External Libraries. Depending on whether the module is in a source folder or a zip/egg file, select either Add source folder or Add zip/jar/egg. Navigate to the site-packages directory and point to the relevant file or folder (mine is: /usr/local/lib/pythonx.x/site-packages)
尝试首选项> pydev >解释器 - python 并删除并重新添加 python 解释器(在删除它之前确保您知道它的路径),当您重新添加它时,请勾选所有框。
Try preferences > pydev > interpreter - python and removing and re-adding the python interpreter (make sure you know the path to it before you delete it), when you re-add it tick all the boxes.
当 Eclipse 对于系统或项目中存在哪些包“丢失”时,从项目的上下文菜单中选择“属性”菜单项,然后选择左侧树形视图中的“PyDev - PYTHONPATH”项对话框,然后是“强制恢复内部信息”按钮。表面上看,PyDev 保留了信息的计算缓存,当由于任何原因缓存变得不一致时,您可以强制 PyDev 重新计算。
When Eclipse gets 'lost' with respect to what packages exists on your system or in your project, from the context menu of your project, choose 'Properties' menu item, then the 'PyDev - PYTHONPATH' item in the treeview on the left of the dialog, then the 'Force restore internal info' button. Seemingly, PyDev keeps a computed cache of the info and when for any reason the cache becomes incoherent, you can force PyDev to recompute.
就我而言,我在编译之前没有收到此错误,但是当我编译时,我收到错误 ImportError:没有名为 myant.core 的模块。我尝试从 PyDev-PYTHONPATH 添加文件,但再次遇到相同的错误。然后我意识到我实际上不必将路径精确添加到我的 .py 文件所在的文件夹中。事实上,我必须添加 myant.core 所在的文件夹。完成此操作后,我再次重新编译项目时重新启动,问题得到解决。我想分享我有:
.py 文件位置:
/${PROJECT_DIR_NAME}/src/myant/core
,因此我添加了 <代码>/${PROJECT_DIR_NAME}/srcIn my case I was not getting this error before compiling, but when I compile I got the error ImportError: No module named myant.core. I tried to add the files from PyDev-PYTHONPATH, but again I got the same error. Then I realized that I actually do not have to add the path exactly to the folder where my .py files are located. Infact I have to add the folder where myant.core is located. After doing this I did a restart when I recompiled my project again, the problem was fixed. I would share that I have:
.py files location:
/${PROJECT_DIR_NAME}/src/myant/core
, therefore I added/${PROJECT_DIR_NAME}/src
@Aufwind,你上面的回答有帮助,但没有解决我的问题。
--->在这里我偏离了你的指示。我为未被识别的模块添加了鸡蛋。添加 site-packages 文件夹并没有修复它。
然后我就可以出发了。 =)
@Aufwind your answer above helped but didn't solve for me.
---> here I departed from your instructions. I added the egg for the module that wasn't being recognized. Adding the site-packages folder didn't fix it.
And then I was good to go. =)
这对我来说是这样。不需要重新启动 Eclipse。
This did it for me. No Eclipse restart was required.
我通过转到项目属性 -> 解决了这个问题PyDev Django
并设置 Django 设置模块。
I fixed this problem by going to the project properties -> PyDev Django
and setting the Django settings module.
对于 Oxygen 2(我认为它也适用于早期版本)...
为了使导入错误标记在我的代码编辑器中消失,我需要在有问题的导入后键入一个空格,然后保存更改。然后导入错误就会消失,因为 PyDev 现在可以找到有问题的导入模块。
For Oxygen 2 (I think it worked on earlier versions, too)...
To make the import error markup disappear in my code editor, I need to type a space after the offending import then save the change. The import error then disappears because PyDev can now find the offending import module.
假设你的永恒模块在/中。
启动 Eclipse 并转到项目选项。选择“PyDev-PYTHONPATH”
在右侧您将看到一个选项卡式窗口。在那里选择外部库。
单击“添加源文件夹”并从上面的路径中选择您的库。
Suppose your eternal module is in /.
Launch Eclipse and go to the project option. Select "PyDev-PYTHONPATH"
and on the right you will see a tabbed window. Select External Libraries there.
Click on Add Source Folder and select your library from the above path.