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:
我在Eclipse中使用了PyDev的Python项目,而PyDev一直在为我的代码生成错误的错误。我有一个定义设置对象的模块设置。我将其导入模块b中,并分配一个属性:
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?
…在Eclipse代码错误窗格中生成“导入:main”的“未定义变量”消息,即使代码运行没有问题。我怎样才能改正这些错误呢?
12 个解决方案
#1
146
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:
要克服这一点,我们有以下几个选择:
-
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 adir(module)
anddir
on the classes found in the module to present completions and make code analysis.如果是某个外部模块,则可以将其添加到强制构建中,这样PyDev生成一个shell以获取运行时信息(详细信息请参见http://pydev.org/manual_101_interpretation .html)。:大多数情况下,PyDev将在shell中导入模块,并在模块中找到的类上执行dir(模块)和dir,以显示完成内容并进行代码分析。
-
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.
您可以在带有错误的行中使用Ctrl+1 (Mac的Cmd+1), PyDev将为您提供添加注释以忽略该错误的选项。
-
It's possible to create a
stub
module and add it to thepredefined
completions (http://pydev.org/manual_101_interpreter.html also has details on that).可以创建一个存根模块并将其添加到预定义的补全中(http://pydev.org/manual_101_interpretation .html也有相关的详细信息)。
#2
45
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.
我在使用opencv,它依赖二进制等等,所以我有脚本,其他行都有这个愚蠢的错误。Python是一种动态语言,因此不应将这种情况视为错误。
I removed these errors altogether by going to:
我把这些错误完全消除了:
Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore
窗口->首选项-> PyDev ->编辑器->代码分析->未定义->未定义变量导入->忽略
And that's that.
那就是了。
It may also be, Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Imports -> Import not found -> Ignore
也可以是,窗口->首选项-> PyDev ->编辑器->代码分析->导入->导入未找到->忽略
#3
11
The post marked as answer gives a workaround, not a solution.
标记为“答案”的帖子给出了一个变通方案,而不是解决方案。
This solution works for me:
这个解决方案对我很有效:
- Go to
Window - Preferences - PyDev - Interpreters - Python Interpreter
- 到窗口首选项- PyDev -解释器- Python解释器。
- Go to the
Forced builtins
tab - 转到“强制构建”选项卡
- Click on
New...
- 点击新…
- Type the name of the module (
multiprocessing
in my case) and clickOK
- 输入模块的名称(在我的例子中是multiprocessing),然后单击OK
Not only will the error messages disappear, the module members will also be recognized.
错误消息不仅会消失,模块成员也会被识别。
#4
7
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.
我在Eclipse/PyDev项目中遇到了类似的问题。在这个项目中,python代码的根目录是项目的子目录。
--> 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.
当项目被调试或运行时,一切正常,因为工作目录被设置为正确的位置。然而,PyDev代码分析没有发现来自module1或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.
解决方案是编辑项目属性——> PyDev - PYTHONPATH节,并从source folders选项卡中删除/MyProject,并向其中添加/MyProject/src。
#5
3
This worked for me:
这工作对我来说:
step 1) Removing the interpreter, auto configuring it again
步骤1)移除解释器,重新配置它。
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
步骤2)窗口-首选项- PyDev -解释器- Python解释器到强制的builtins选项卡上单击New…键入模块的名称(在我的例子中是诅咒),然后单击OK。
step 3) Right click in the project explorer on whichever module is giving errors. Go to PyDev->Code analysis.
步骤3)在项目资源管理器中右键单击出现错误的模块。PyDev中- >代码分析。
#6
1
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).
我也有同样的问题。我正在Windows上使用Python和Eclipse。代码运行良好,但是eclipse到处都显示错误。当我将文件夹“Lib”的名称更改为“Lib”(C:\Python27\ Lib)后,问题就解决了。如果字母的大小写与配置文件中的大小写不匹配,有时会导致问题(但似乎并非总是如此,因为错误检查在问题突然出现之前很长一段时间内都没有问题)。
#7
1
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)
对此pylint说:E: 4,15:未定义的变量“子模块”(未定义的变量)
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.
注意:我还通过导入的变量重命名为大写,以反映其常量属性。
#8
0
It is possible you just need to re-configure your python path within Eclipse. See my answer to a similar question.
您可能只需要在Eclipse中重新配置python路径。看看我对类似问题的回答。
#9
0
in preferences --> PyDev --> PyLint under arguments to pass to PyLint add this line:
在参数设置中——> PyDev——在参数传递到PyLint的参数下,将这一行添加到PyLint中:
--generated-members=objects
you will need to do this for each generated . I found this by googling, but I lost the reference.
您将需要为生成的每个文件执行此操作。我是通过谷歌找到的,但是我丢失了参考资料。
#10
0
Right click in the project explorer on whichever module is giving errors. Go to PyDev->Remove Error Markers.
在项目资源管理器中右键单击哪个模块出现错误。转到PyDev->删除错误标记。
#11
0
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()"
PyDev显示了错误消息“导入的未定义变量:get_default_root_window()”
In the python shell you can see that this is a 'built-in' module as mentioned in a answer above:
在python shell中,您可以看到这是一个“内置”模块,如上面的回答所述:
>>> 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.
现在在窗口->偏好->PyDev->解释器->Python解释器,我选择了标签“强制构建”并添加了“gtk”。gdk”列表中。
Now the error message didn't show anymore.
现在错误信息不再显示了。
#12
0
I find that these 2 steps work for me all the time:
我发现这两个步骤一直对我有效:
- Confirm (else add) the parent folder of the module to the PYTHONPATH.
- 确认(否则)模块的父文件夹到PYTHONPATH中。
- 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.
一些流行的模块安装时,父模块和子模块的名称相同。在这些情况下,您还必须将父目录添加到PYTHONPATH中,并添加到它的祖父母文件夹中,您已经为其他所有内容确认/添加了这个文件夹。
-
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.
使用(例如)“google.appengine.api。当添加到强制构建的时候,“memcache”,而不是“memcache”(在本例中,“谷歌”是在PYTHONPATH中定义的文件夹的直接子文件夹)。
#1
146
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:
要克服这一点,我们有以下几个选择:
-
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 adir(module)
anddir
on the classes found in the module to present completions and make code analysis.如果是某个外部模块,则可以将其添加到强制构建中,这样PyDev生成一个shell以获取运行时信息(详细信息请参见http://pydev.org/manual_101_interpretation .html)。:大多数情况下,PyDev将在shell中导入模块,并在模块中找到的类上执行dir(模块)和dir,以显示完成内容并进行代码分析。
-
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.
您可以在带有错误的行中使用Ctrl+1 (Mac的Cmd+1), PyDev将为您提供添加注释以忽略该错误的选项。
-
It's possible to create a
stub
module and add it to thepredefined
completions (http://pydev.org/manual_101_interpreter.html also has details on that).可以创建一个存根模块并将其添加到预定义的补全中(http://pydev.org/manual_101_interpretation .html也有相关的详细信息)。
#2
45
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.
我在使用opencv,它依赖二进制等等,所以我有脚本,其他行都有这个愚蠢的错误。Python是一种动态语言,因此不应将这种情况视为错误。
I removed these errors altogether by going to:
我把这些错误完全消除了:
Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Undefined -> Undefined Variable From Import -> Ignore
窗口->首选项-> PyDev ->编辑器->代码分析->未定义->未定义变量导入->忽略
And that's that.
那就是了。
It may also be, Window -> Preferences -> PyDev -> Editor -> Code Analysis -> Imports -> Import not found -> Ignore
也可以是,窗口->首选项-> PyDev ->编辑器->代码分析->导入->导入未找到->忽略
#3
11
The post marked as answer gives a workaround, not a solution.
标记为“答案”的帖子给出了一个变通方案,而不是解决方案。
This solution works for me:
这个解决方案对我很有效:
- Go to
Window - Preferences - PyDev - Interpreters - Python Interpreter
- 到窗口首选项- PyDev -解释器- Python解释器。
- Go to the
Forced builtins
tab - 转到“强制构建”选项卡
- Click on
New...
- 点击新…
- Type the name of the module (
multiprocessing
in my case) and clickOK
- 输入模块的名称(在我的例子中是multiprocessing),然后单击OK
Not only will the error messages disappear, the module members will also be recognized.
错误消息不仅会消失,模块成员也会被识别。
#4
7
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.
我在Eclipse/PyDev项目中遇到了类似的问题。在这个项目中,python代码的根目录是项目的子目录。
--> 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.
当项目被调试或运行时,一切正常,因为工作目录被设置为正确的位置。然而,PyDev代码分析没有发现来自module1或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.
解决方案是编辑项目属性——> PyDev - PYTHONPATH节,并从source folders选项卡中删除/MyProject,并向其中添加/MyProject/src。
#5
3
This worked for me:
这工作对我来说:
step 1) Removing the interpreter, auto configuring it again
步骤1)移除解释器,重新配置它。
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
步骤2)窗口-首选项- PyDev -解释器- Python解释器到强制的builtins选项卡上单击New…键入模块的名称(在我的例子中是诅咒),然后单击OK。
step 3) Right click in the project explorer on whichever module is giving errors. Go to PyDev->Code analysis.
步骤3)在项目资源管理器中右键单击出现错误的模块。PyDev中- >代码分析。
#6
1
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).
我也有同样的问题。我正在Windows上使用Python和Eclipse。代码运行良好,但是eclipse到处都显示错误。当我将文件夹“Lib”的名称更改为“Lib”(C:\Python27\ Lib)后,问题就解决了。如果字母的大小写与配置文件中的大小写不匹配,有时会导致问题(但似乎并非总是如此,因为错误检查在问题突然出现之前很长一段时间内都没有问题)。
#7
1
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)
对此pylint说:E: 4,15:未定义的变量“子模块”(未定义的变量)
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.
注意:我还通过导入的变量重命名为大写,以反映其常量属性。
#8
0
It is possible you just need to re-configure your python path within Eclipse. See my answer to a similar question.
您可能只需要在Eclipse中重新配置python路径。看看我对类似问题的回答。
#9
0
in preferences --> PyDev --> PyLint under arguments to pass to PyLint add this line:
在参数设置中——> PyDev——在参数传递到PyLint的参数下,将这一行添加到PyLint中:
--generated-members=objects
you will need to do this for each generated . I found this by googling, but I lost the reference.
您将需要为生成的每个文件执行此操作。我是通过谷歌找到的,但是我丢失了参考资料。
#10
0
Right click in the project explorer on whichever module is giving errors. Go to PyDev->Remove Error Markers.
在项目资源管理器中右键单击哪个模块出现错误。转到PyDev->删除错误标记。
#11
0
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()"
PyDev显示了错误消息“导入的未定义变量:get_default_root_window()”
In the python shell you can see that this is a 'built-in' module as mentioned in a answer above:
在python shell中,您可以看到这是一个“内置”模块,如上面的回答所述:
>>> 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.
现在在窗口->偏好->PyDev->解释器->Python解释器,我选择了标签“强制构建”并添加了“gtk”。gdk”列表中。
Now the error message didn't show anymore.
现在错误信息不再显示了。
#12
0
I find that these 2 steps work for me all the time:
我发现这两个步骤一直对我有效:
- Confirm (else add) the parent folder of the module to the PYTHONPATH.
- 确认(否则)模块的父文件夹到PYTHONPATH中。
- 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.
一些流行的模块安装时,父模块和子模块的名称相同。在这些情况下,您还必须将父目录添加到PYTHONPATH中,并添加到它的祖父母文件夹中,您已经为其他所有内容确认/添加了这个文件夹。
-
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.
使用(例如)“google.appengine.api。当添加到强制构建的时候,“memcache”,而不是“memcache”(在本例中,“谷歌”是在PYTHONPATH中定义的文件夹的直接子文件夹)。