标签归档:pydev

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

问题:如何修复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.

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


PyDev和Eclipse的未解决的导入问题

问题:PyDev和Eclipse的未解决的导入问题

我对PyDev和Python还是很陌生,尽管我已经大量使用Eclipse来编写Java。我正在尝试研究一些“ Dive Into Python”示例,这感觉像一个极其琐碎的问题,变得非常烦人。我正在使用Ubuntu Linux 10.04。

我希望能够使用odbchelper.py文件,该文件位于目录中 /Desktop/Python_Tutorials/diveintopython/py

这是我在PyDev / Eclipse项目中正在使用的example.py文件:

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()功能将该目录添加到我的路径,然后将其导入…,但这恰恰是我在代码中所做的,并且不起作用。

我也尝试了Ctrl1技巧来抑制错误消息,但该程序仍无法正常运行。我收到一个错误ImportError: No module named 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 Ctrl1 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.


回答 0

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


回答 1

我正在使用eclipse kepler 4.3,PyDev 3.9.2和在Ubuntu 14.04上遇到相同的问题。我尝试并花费了数小时,使用了以上所有大多数选项,但徒劳无功。然后我尝试了以下很棒的方法:

  • 选择项目 – > RightClick-> 的PyDev – > 删除PyDev的项目配置
  • 文件-> 重新启动

我认为我使用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:

  • Select Project-> RightClick-> PyDev-> Remove PyDev Project Config
  • file-> restart

And I was using Python 2.7 as an interpreter, although it doesn’t effect, I think.


回答 2

我刚刚将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!


回答 3

我修复了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.


回答 4

项目->属性-> pydev-pythonpath->外部库->添加源文件夹,添加项目的父文件夹。然后重新启动Eclipse。

project–>properties–>pydev-pythonpath–>external libraries –> add source folder, add the PARENT FOLDER of the project. Then restart eclipse.


回答 5

这是为我工作的(由soulBit推荐):

1) Restart using restart from the file menu
2) Once it started again, manually close and open it.

这是有史以来最简单的解决方案,它完全消除了烦人的事情。

Here is what worked for me (sugested by soulBit):

1) Restart using restart from the file menu
2) Once it started again, manually close and open it.

This is the simplest solution ever and it completely removes the annoying thing.


回答 6

有两种方法可以解决此问题:

  • 从“ Python解释器”中删除Python解释器,然后再次添加。
  • 或者只是在项目中使用的解释器中添加包含库的文件夹,就我而言,我使用的是“ bottle”,添加的文件夹是“ c:\ Python33 \ Lib \ site-packages \ bottle-0.11.6 -py3.3.egg”

现在,我不再看到该错误,并且代码完成功能也可以与“瓶”一起使用。

There are two ways of solving this issue:

  • Delete the Python interpreter from “Python interpreters” and add it again.
  • Or just add the folder with the libraries in the interpreter you are using in your project, in my case I was using “bottle” and the folder I added was “c:\Python33\Lib\site-packages\bottle-0.11.6-py3.3.egg”

Now I don’t see the error anymore, and the code completion feature works as well with “bottle”.


回答 7

我正在运行Eclipse 4.2.0(Juno)和PyDev 2.8.1,并在将lib安装到我的站点软件包路径时遇到了此问题。根据这个SO问题:

Pydev和* .pyc文件

… PyDev和pyc文件存在问题。对于我尝试引用的特定库,仅提供pyc文件。

这是我为解决此问题所做的工作:

  1. https://github.com/Mysterie/uncompyle2安装uncompyle2
  2. 对site-packages库中的* .pyc文件运行uncompyle2。例:

    uncompyle2 -r -o / tmp / path / to / site-packages / lib

  3. 将由uncompyle2生成的结果* .pyc_dis文件重命名为* .py
  4. 将这些* .py文件移动/复制到站点软件包路径
  5. 在Eclipse中,选择File> Restart

与.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:

  1. Install uncompyle2 from https://github.com/Mysterie/uncompyle2
  2. Run uncompyle2 against the *.pyc files in the site-packages lib. Example:

    uncompyle2 -r -o /tmp /path/to/site-packages/lib

  3. Rename the resulting *.pyc_dis files produced from uncompyle2 to *.py
  4. Move / copy these *.py files to the site-packages path
  5. In Eclipse, select File > Restart

The unresolved import error relating to .pyc files should now disappear.


回答 8

以下,我认为会解决问题

  1. init .py 添加到“〜/ Desktop / Python_Tutorials / diveintopython / py”文件夹中
  2. 转到窗口->首选项-> PyDev->解释器-> Python解释器以删除您的Python解释器设置(原因是因为PyDev无法自动刷新对任何系统PythonPath所做的任何更新)
  3. 使用与之前相同的详细信息添加解释器(这将通过对PythonPath进行的更新来刷新Python解释器设置)
  4. 最后,由于您的“〜/ Desktop / Python_Tutorials / diveintopython / py”文件夹不是标准的PythonPath,因此需要将其添加。有两种方法可以实现

一个。按照大卫·德文的建议。但是,这仅适用于您所在的特定项目。在“窗口”->首选项-> PyDev->解释器-> Python解释器->库子选项卡-> NewFolder下,将“〜/ Desktop / Python_Tutorials / diveintopython / py”添加到新的PythonPath中

希望能帮助到你。

Following, in my opinion will solve the problem

  1. Adding the init.py to your “~/Desktop/Python_Tutorials/diveintopython/py” folder
  2. Go to Window –> Preferences –> PyDev –> Interpreters –> Python Interpreter to remove your Python Interpreter setting (reason being is because PyDev unable to auto refresh any updates made to any System PythonPath)
  3. Add in the Interpreter with the same details as before (this will refresh your Python Interpreter setting with updates made to your PythonPath)
  4. Finally since your “~/Desktop/Python_Tutorials/diveintopython/py” folder not a standard PythonPath, you will need to add it in. There are two ways to do it

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.


回答 9

在尝试解决问题之后,通过理解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.


回答 10

KD.py

class A:
a=10;

KD2.py 
from com.jbk.KD import A;
class B:
  b=120;

aa=A();
print(aa.a)

这对我来说很完美

另一个例子是

main.py
=======
from com.jbk.scenarios.objectcreation.settings import _init
from com.jbk.scenarios.objectcreation.subfile import stuff

_init();
stuff();

settings.py
==========
def _init():
print("kiran")


subfile.py
==========
def stuff():
print("asasas")    
KD.py

class A:
a=10;

KD2.py 
from com.jbk.KD import A;
class B:
  b=120;

aa=A();
print(aa.a)

THIS works perfectly file for me

Another example is

main.py
=======
from com.jbk.scenarios.objectcreation.settings import _init
from com.jbk.scenarios.objectcreation.subfile import stuff

_init();
stuff();

settings.py
==========
def _init():
print("kiran")


subfile.py
==========
def stuff():
print("asasas")