在eclipse中将python模块添加到pydev会导致导入错误?

发布于 2024-11-08 11:16:22 字数 976 浏览 1 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(11

ぃ弥猫深巷。 2024-11-15 11:16:22

这是我的问题的解决方案:

  1. 找出相应 python 版本的文件夹 ../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.)
  2. 打开 Eclipse 首选项并转到 PyDev -> 解释器 - Python
  3. 在下框的左侧,单击新建文件夹
  4. 添加导航到相应 python 版本的 ../site-packages/
  5. 点击打开
  6. 点击应用
  7. 点击确定

你应该可以走了。 =)

感谢@allparticionts,他们在评论中提供了正确方向的提示。

This is the solution to my problem:

  1. Find out the path to the folder ../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.)
  2. Open eclipse preferences and go to PyDev -> Interpreter - Python.
  3. On the left side of the lower box, click on New Folder.
  4. Add the navigate to ../site-packages/ of your corresponding python version.
  5. Hit open.
  6. Hit Apply.
  7. Hit Ok.

And you should be good to go. =)

Thanks @all particionts, who provided hints into the right direction in the comments.

征﹌骨岁月お 2024-11-15 11:16:22

我今天遇到了同样的问题。我正在使用 pydev 并有一个包含许多子包的工作项目。创建新模块后突然我无法在不同的包中使用该模块。令人费解的功能是我可以在同一个子包中使用另一个模块...

最后,在

  1. eclipse 重新启动
  2. 后,删除/添加 python 解释器和所有站点包都
  3. 令人头疼,

我使用以下脚本删除了所有已编译的类:

import os

def clean_folder(folder):
  for file in os.listdir(folder):
    path = os.path.join(folder,file)
    if os.path.isdir(path):
        clean_folder(path)

    if '.pyc' == file[-4:]:
        print 'deleting: ' + str(path)
        os.remove(path)

if __name__ == '__main__':
  folder = 'YOUR_PROJECT_SRC_PATH'
  clean_folder(folder)

最后我可以做“实际”工作:)
希望它能帮助某人...

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

  1. eclipse restart
  2. remove/add python interpreter and all site-packages
  3. annoyed head-scratching

I deleted all compiled classes with the following script:

import os

def clean_folder(folder):
  for file in os.listdir(folder):
    path = os.path.join(folder,file)
    if os.path.isdir(path):
        clean_folder(path)

    if '.pyc' == file[-4:]:
        print 'deleting: ' + str(path)
        os.remove(path)

if __name__ == '__main__':
  folder = 'YOUR_PROJECT_SRC_PATH'
  clean_folder(folder)

and finally I can do 'actual' work :)
Hope it helps somebody...

复古式 2024-11-15 11:16:22

您只需将模块添加到 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)

故乡的云 2024-11-15 11:16:22

尝试首选项> 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.

梦里的微风 2024-11-15 11:16:22

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

壹場煙雨 2024-11-15 11:16:22

就我而言,我在编译之前没有收到此错误,但是当我编译时,我收到错误 ImportError:没有名为 myant.core 的模块。我尝试从 PyDev-PYTHONPATH 添加文件,但再次遇到相同的错误。然后我意识到我实际上不必将路径精确添加到我的 .py 文件所在的文件夹中。事实上,我必须添加 myant.core 所在的文件夹。完成此操作后,我再次重新编译项目时重新启动,问题得到解决。我想分享我有:

Python 2.7 Eclipse kepler 4.3、PyDev 3.9.2 和我的 ubuntu 14.04

.py 文件位置:/${PROJECT_DIR_NAME}/src/myant/core,因此我添加了 <代码>/${PROJECT_DIR_NAME}/src

In 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:

Python 2.7 Eclipse kepler 4.3, PyDev 3.9.2 and on my ubuntu 14.04

.py files location:/${PROJECT_DIR_NAME}/src/myant/core, therefore I added /${PROJECT_DIR_NAME}/src

墨小沫ゞ 2024-11-15 11:16:22

@Aufwind,你上面的回答有帮助,但没有解决我的问题。

  • 找到文件夹 ../site-packages/ ... 的路径。
  • 打开 eclipse 首选项并转到 PyDev ->解释器——Python。
  • 在下框的左侧,单击“新建文件夹”。
    --->在这里我偏离了你的指示。我为未被识别的模块添加了鸡蛋。添加 site-packages 文件夹并没有修复它。
  • 点击打开。
  • 点击“应用”。
  • 点击“确定”。

然后我就可以出发了。 =)

@Aufwind your answer above helped but didn't solve for me.

  • Find the path to the folder ../site-packages/ ....
  • Open eclipse preferences and go to PyDev -> Interpreter - Python.
  • On the left side of the lower box, click on New Folder.
    ---> 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.
  • Hit open.
  • Hit Apply.
  • Hit Ok.

And then I was good to go. =)

小瓶盖 2024-11-15 11:16:22
  1. 打开eclipse窗口->首选项并转到 PyDev ->口译员。
  2. 单击“检查解释器是否与环境同步”

这对我来说是这样。不需要重新启动 Eclipse。

  1. Open eclipse window -> preferences and go to PyDev -> Interpreter.
  2. click on 'Check if interpreters are synchronized with environment'

This did it for me. No Eclipse restart was required.

策马西风 2024-11-15 11:16:22

我通过转到项目属性 -> 解决了这个问题PyDev Django
并设置 Django 设置模块。在此处输入图像描述

I fixed this problem by going to the project properties -> PyDev Django
and setting the Django settings module.enter image description here

始终不够爱げ你 2024-11-15 11:16:22

对于 Oxygen 2(我认为它也适用于早期版本)...

  1. 右键单击​​项目文件夹并选择“属性”
  2. 选择“PyDev - 解释器/语法”
  3. 单击“单击此处配置解释器未列出”
  4. 从已配置解释器的顶部列表中选择任何现有解释器
  5. 将出现“需要选择”对话框,您必须在其中选择一个或多个要恢复的解释器。检查所有适用的
  6. 单击“确定”,PyDev 将重新扫描,我假设重建站点包的一些内部视图
  7. 单击“应用并关闭”关闭所有对话框

为了使导入错误标记在我的代码编辑器中消失,我需要在有问题的导入后键入一个空格,然后保存更改。然后导入错误就会消失,因为 PyDev 现在可以找到有问题的导入模块。

For Oxygen 2 (I think it worked on earlier versions, too)...

  1. Right click on project folder and select "Properties"
  2. Select "PyDev - Interpreter/Grammar"
  3. Click on "Click here to configure an interpreter not listed"
  4. Select any existing interpreter from the top list of configured interpreters
  5. A "Selection Needed" dialog should appear where you must select one or more interpreters to restore. Check all that apply
  6. Click "Ok" and PyDev will rescan, and I assume, rebuild some internal view of your site-packages
  7. Click "Apply and Close" to close all dialogs

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.

幸福%小乖 2024-11-15 11:16:22

假设你的永恒模块在/中。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文