问题:如何修复PyDev“导入时未定义变量”错误?

我有一个在Eclipse中使用PyDev的Python项目,并且PyDev不断为我的代码生成错误错误。我有一个settings定义settings对象的模块。我将其导入模块b并使用以下属性分配属性:

from settings import settings
settings.main = object()

在我的一些代码(但不是全部)中,语句如下:

from settings import settings
print settings.main 

…即使在代码运行没有问题的情况下,也会在Eclipse代码错误窗格中生成“从import:main导入未定义的变量”消息。我该如何纠正?

I’ve got a Python project using PyDev in Eclipse, and PyDev keeps generating false errors for my code. I have a module settings that defines a settings object. I import that in module b and assign an attribute with:

from settings import settings
settings.main = object()

In some of my code–but not all of it, statements like:

from settings import settings
print settings.main 

… generate “Undefined variable from import: main” messages in the Eclipse code error pane, even though the code runs without a problem. How can I correct these?


回答 0

对于您项目中的代码,唯一的方法是添加一个声明,声明您希望-可能受到a的保护,if False从而使其无法执行(如果您执行了静态代码分析,则只能看到您看到的内容,而不能看到运行时信息)自己打开该模块,则不会显示预期为main。

为了克服这个问题,有一些选择:

  1. 如果它是某个外部模块,则可以将其添加到,forced builtins以便PyDev为其生成外壳以获取运行时信息(有关详细信息,请参见http://pydev.org/manual_101_interpreter.html),即:大多数情况下,PyDev会将模块导入外壳中,并对模块中找到的类进行dir(module)dir,以显示完成内容并进行代码分析。

  2. 您可以在出现错误的行中使用Ctrl + 1(对于Mac为Cmd + 1),PyDev将为您提供一个添加注释的选项,以忽略该错误。

  3. 可以创建一个stub模块并将其添加到补全中predefinedhttp://pydev.org/manual_101_interpreter.html上也有详细信息)。

For code in your project, the only way is adding a declaration saying that you expected that — possibly protected by an if False so that it doesn’t execute (the static code-analysis only sees what you see, not runtime info — if you opened that module yourself, you’d have no indication that main was expected).

To overcome this there are some choices:

  1. If it is some external module, it’s possible to add it to the forced builtins so that PyDev spawns a shell for it to obtain runtime information (see http://pydev.org/manual_101_interpreter.html for details) — i.e.: mostly, PyDev will import the module in a shell and do a dir(module) and dir on the classes found in the module to present completions and make code analysis.

  2. You can use Ctrl+1 (Cmd+1 for Mac) in a line with an error and PyDev will present you an option to add a comment to ignore that error.

  3. It’s possible to create a stub module and add it to the predefined completions (http://pydev.org/manual_101_interpreter.html also has details on that).


回答 1

我正在使用opencv,它依赖于二进制文件等,所以我有脚本,其中每隔一行都有这个愚蠢的错误。Python是一种动态语言,因此不应将这种情况视为错误。

我通过以下步骤彻底删除了这些错误:

窗口->首选项-> PyDev->编辑器->代码分析->未定义->来自导入的未定义变量->忽略

就是这样。

也可能是,窗口->首选项-> PyDev->编辑器->代码分析->导入->找不到导入->忽略

I’m using opencv which relies on binaries etc so I have scripts where every other line has this silly error. Python is a dynamic language so such occasions shouldn’t be considered errors.

I removed these errors altogether by going to:

Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore

And that’s that.

It may also be, Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Imports -> Import not found -> Ignore


回答 2

标为答案的帖子提供了一种解决方法,而不是解决方案。

此解决方案适用于我:

  • Window - Preferences - PyDev - Interpreters - Python Interpreter
  • 转到Forced builtins标签
  • 点击 New...
  • 输入模块名称(multiprocessing以我为例),然后单击OK

错误消息不仅将消失,而且模块成员也将被识别。

The post marked as answer gives a workaround, not a solution.

This solution works for me:

  • Go to Window - Preferences - PyDev - Interpreters - Python Interpreter
  • Go to the Forced builtins tab
  • Click on New...
  • Type the name of the module (multiprocessing in my case) and click OK

Not only will the error messages disappear, the module members will also be recognized.


回答 3

我在Eclipse / PyDev项目中遇到类似的问题。在这个项目中,python代码的根目录是项目的子目录。

--> MyProject
 + --> src         Root of python code
   + --> module1     A module 
   + --> module2     Another module
 + --> docs
 + --> test

在调试或运行项目时,一切都很好,因为工作目录已设置到正确的位置。但是,PyDev代码分析未能找到来自module1或module2的任何导入。

解决方案是编辑项目属性-> PyDev-PYTHONPATH部分,然后从“源文件夹”选项卡中删除/ MyProject,然后向其中添加/ MyProject / src。

I was having a similar problem with an Eclipse/PyDev project. In this project the root directory of the python code was a sub-directory of the project.

--> MyProject
 + --> src         Root of python code
   + --> module1     A module 
   + --> module2     Another module
 + --> docs
 + --> test

When the project was debugged or run everything was fine as the working directory was set to the correct place. However the PyDev code analysis was failing to find any imports from module1 or module2.

Solution was to edit the project properties -> PyDev – PYTHONPATH section and remove /MyProject from the source folders tab and add /MyProject/src to it instead.


回答 4

这对我有用:

步骤1)移除解释器,再次自动配置

步骤2)窗口-首选项-PyDev-解释器-Python解释器转到“强制内置”选项卡。单击“新建…”。输入模块名称(在我的情况下为curses),然后单击“确定”。

步骤3)右键单击项目浏览器中出现错误的模块。转到PyDev->代码分析。

This worked for me:

step 1) Removing the interpreter, auto configuring it again

step 2) Window – Preferences – PyDev – Interpreters – Python Interpreter Go to the Forced builtins tab Click on New… Type the name of the module (curses in my case) and click OK

step 3) Right click in the project explorer on whichever module is giving errors. Go to PyDev->Code analysis.


回答 5

我有同样的问题。我在Windows上使用Python和Eclipse。该代码运行得很好,但是Eclipse到处都会显示错误。将文件夹“ Lib”的名称更改为“ lib”(C:\ Python27 \ lib)之后,问题得以解决。看来,如果字母的大写字母与配置文件中的字母不匹配,这有时会引起问题(但似乎并不总是这样,因为错误检查很长一段时间就可以在问题突然出现之前很明显了原因)。

I had the same problem. I am using Python and Eclipse on Windows. The code was running just fine, but eclipse show errors everywhere. After I changed the name of the folder ‘Lib’ to ‘lib’ (C:\Python27\lib), the problem was solved. It seems that if the capitalization of the letters doesn’t match the one in the configuration file, this will sometimes cause problems (but it seems like not always, because the error checking was fine for long time before the problems suddenly appeared for no obvious reason).


回答 6

我正在做什么的一个近似值:

import module.submodule

class MyClass:
    constant = submodule.constant

皮林特说: E: 4,15: Undefined variable 'submodule' (undefined-variable)

我通过更改导入来解决此问题,例如:

from module.submodule import CONSTANT

class MyClass:
    constant = CONSTANT

注意:我还使用导入的变量重命名了名称,以反映其常量性质。

An approximation of what I was doing:

import module.submodule

class MyClass:
    constant = submodule.constant

To which pylint said: E: 4,15: Undefined variable 'submodule' (undefined-variable)

I resolved this by changing my import like:

from module.submodule import CONSTANT

class MyClass:
    constant = CONSTANT

Note: I also renamed by imported variable to have an uppercase name to reflect its constant nature.


回答 7

您可能只需要在Eclipse中重新配置python路径。见我的回答对一个类似问题。

It is possible you just need to re-configure your python path within Eclipse. See my answer to a similar question.


回答 8

在首选项-> PyDev-> PyLint 传递给PyLint的参数下添加以下行:

--generated-members=objects

您将需要针对每个生成的进行此操作。我通过谷歌搜索找到了这个,但是我丢失了参考。

in preferences –> PyDev –> PyLint under arguments to pass to PyLint add this line:

--generated-members=objects

you will need to do this for each generated . I found this by googling, but I lost the reference.


回答 9

在项目浏览器中右键单击出现错误的模块。转到PyDev->删除错误标记。

Right click in the project explorer on whichever module is giving errors. Go to PyDev->Remove Error Markers.


回答 10

我的回答没有任何新贡献,只是我遇到的一个具体例子。

import gtk.gdk

w = gtk.gdk.get_default_root_window()

PyDev显示错误消息“来自导入的未定义变量:get_default_root_window()”

在python shell中,您可以看到这是一个“内置”模块,如上面的答案所述:

>>> import gtk.gdk
>>> gtk.gdk
<module 'gtk.gdk' (built-in)>

现在,在Window-> Preferences-> PyDev-> Interpreters-> Python Interpreter下,选择选项卡“ Forced Builtins”并将“ gtk.gdk”添加到列表中。

现在错误消息不再显示。

My answer doesn’t contribute anything new, just a concrete example I encountered.

import gtk.gdk

w = gtk.gdk.get_default_root_window()

PyDev showed the error message “Undefined variable from import: get_default_root_window()”

In the python shell you can see that this is a ‘built-in’ module as mentioned in a answer above:

>>> import gtk.gdk
>>> gtk.gdk
<module 'gtk.gdk' (built-in)>

Now under Window->Preferences->PyDev->Interpreters->Python Interpreter, I selected the tab ‘Forced Builtins’ and added ‘gtk.gdk’ to the list.

Now the error message didn’t show anymore.


回答 11

我发现这两个步骤一直对我有用:

  1. 确认(否则添加)模块的父文件夹到PYTHONPATH。
  2. 将模块的全名添加到强制内置。

这里要注意的事情:

  • 一些流行的模块会安装一些具有相同名称的父对子对。在这些情况下,除了已确认/添加的其他父项文件夹之外,还必须将该父项添加到PYTHONPATH中。

  • 当添加到强制内置时,请使用(例如)“ google.appengine.api.memcache”,而不是仅使用“ memcache”,在此示例中,“ google”是PYTHONPATH中定义的文件夹的直接子代。

I find that these 2 steps work for me all the time:

  1. Confirm (else add) the parent folder of the module to the PYTHONPATH.
  2. Add FULL name of the module to forced builtins.

The things to note here:

  • Some popular modules install with some parent and child pair having the same name. In these cases you also have to add that parent to PYTHONPATH, in addition to its grandparent folder, which you already confirmed/added for everything else.

  • Use (for example) “google.appengine.api.memcache” when adding to forced builtins, NOT “memcache” only, where “google” in this example, is an immediate child of a folder defined in PYTHONPATH.


回答 12

如果确定脚本已运行并且是错误警报,请转至“首选项”>“ PyDev”>“编辑器”>“代码分析”。将错误降级为警告。

在此处输入图片说明

http://www.pydev.org/manual_adv_code_analysis.html

If you’re sure that your script runs and that it is a false alarm, Go to Preferences > PyDev > Editor > Code Analysis. Demote the errors to warnings.

enter image description here

http://www.pydev.org/manual_adv_code_analysis.html


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。