在 eclipse pydev 中导入 numpy 时出现“未定义变量来自导入”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13842194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Undefined variable from import" when importing numpy in eclipse pydev
提问by user1898037
I get "Undefined variable from import" error in Eclipse 3.7.2 PyDev 2.7.1 on Linux Mint Maya (Cinnamon) whenever I import a module from numpy. The problem only arises when importing numpy; other modules work just fine.
每当我从 numpy 导入模块时,我都会在 Linux Mint Maya (Cinnamon) 上的 Eclipse 3.7.2 PyDev 2.7.1 中收到“来自导入的未定义变量”错误。问题只在导入numpy时出现;其他模块工作得很好。
Info: numpy is compiled against the Intel MKL libraries. Python 2.7.3 [GCC 4.6.3]
信息:numpy 是针对英特尔 MKL 库编译的。Python 2.7.3 [GCC 4.6.3]
example:
例子:
import numpy as np
a = np.array([1, 2])
Error message in PyDev editor: "Undefined variable from import: array"
PyDev 编辑器中的错误消息:“来自导入的未定义变量:数组”
I get similar errors for other numpy submodules.
对于其他 numpy 子模块,我也遇到了类似的错误。
The code runs properly either within Eclipse or in a terminal. The problem is only present in the Eclipse-PyDev editor.
代码在 Eclipse 或终端中正常运行。该问题仅存在于 Eclipse-PyDev 编辑器中。
采纳答案by user1898037
The LD_LIBRARY_PATH
has to contain paths to the MKL libraries. Even if LD_LIBRARY_PATH
is set properly, it has to be explicitly specifed in Eclipse under Window > Preferences > Interpreter - Python, under the "Environment" tab on the right hand side. Add a new variable called "LD_LIBRARY_PATH" and paste the content of $LD_LIBRARY_PATH
.
在LD_LIBRARY_PATH
必须包含路径的MKL库。即使LD_LIBRARY_PATH
设置正确,也必须在 Eclipse 中的 Window > Preferences > Interpreter - Python 下的右侧“环境”选项卡下明确指定。添加一个名为“LD_LIBRARY_PATH”的新变量并粘贴$LD_LIBRARY_PATH
.
Once it is done, errors will only disappear from your code if a line is added to the code which uses a numpy object so that imports are checked again.
完成后,只有在使用 numpy 对象的代码中添加一行以便再次检查导入时,错误才会从您的代码中消失。
回答by Dave Novelli
Are MKL libraries and numpy the same thing? @user1898039's answer didn't make sense to me (I'm not sure what path I should use for that property.
MKL 库和 numpy 是一回事吗?@user1898039 的回答对我来说没有意义(我不确定我应该为该属性使用什么路径。
I found another way to fix this issue - I checked the python command line to see if numpy was installed there by calling:
我找到了解决此问题的另一种方法 - 我检查了 python 命令行,通过调用以下命令查看是否安装了 numpy:
>>> import from numpy *
>>> import from numpy *
It was not. So, despite the fact that numpy was showing up in PyDev under the "Forced Builtins" tab in the Python Interpreter properties, it wasn't available to python from the command line and I imagine this caused the issue inside PyDev.
不是。因此,尽管 numpy 出现在 Python 解释器属性的“强制内置”选项卡下的 PyDev 中,但它无法从命令行用于 python,我想这会导致 PyDev 内部出现问题。
So, I fixed the problem by going to the numpy site and installing the newest version for windows. Technically I went to some other site that had a AMD64 version of the installer for windows, buy you know, same same. After that I tried to get the PYTHONPATH variable to recalculate all the libraries (this may have been unnecessary), but errors were still showing in the file on all the calls to array(). I did an autocomplete on the numpy package to get the list of functions, selected array() from that list, and all of the sudden all the errors cleared and everything is fine now.
因此,我通过访问 numpy 站点并安装适用于 Windows 的最新版本来解决该问题。从技术上讲,我去了其他一些网站,那里有一个 AMD64 版本的 Windows 安装程序,买你知道的,同样的。之后,我尝试让 PYTHONPATH 变量重新计算所有库(这可能是不必要的),但文件中仍然显示所有对 array() 调用的错误。我在 numpy 包上做了一个自动完成来获取函数列表,从该列表中选择 array(),突然间所有的错误都被清除了,现在一切都很好。
回答by illaiza
I managed to fix it by updating the Phyton interpreter in Eclipse. Just click on Apply button after successfully installed Numpy module.
我设法通过更新 Eclipse 中的 Phyton 解释器来修复它。成功安装 Numpy 模块后,只需单击“应用”按钮即可。
回答by miraculixx
I was able to resolve this by adding the following path to PYTHONPATH (Pydev => Python Interpreters => select an Interpreter => Libraries):
我能够通过将以下路径添加到 PYTHONPATH(Pydev => Python Interpreters => select an Interpreter => Libraries)来解决这个问题:
/usr/local/anaconda/lib/python2.7/lib/lib-dynload
installed packages (among others):
已安装的软件包(除其他外):
conda==3.10.1
numpy==1.9.2
scipy==0.15.1