“Undefined variable from import” when importing numpy in eclipse pydev

一曲冷凌霜 提交于 2019-12-06 21:41:21

问题


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.

Info: numpy is compiled against the Intel MKL libraries. 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"

I get similar errors for other numpy submodules.

The code runs properly either within Eclipse or in a terminal. The problem is only present in the Eclipse-PyDev editor.


回答1:


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.

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.




回答2:


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.

I found another way to fix this issue - I checked the python command line to see if numpy was installed there by calling:

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

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.




回答3:


I managed to fix it by updating the Phyton interpreter in Eclipse. Just click on Apply button after successfully installed Numpy module.




回答4:


I was able to resolve this by adding the following path to 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


来源:https://stackoverflow.com/questions/13842194/undefined-variable-from-import-when-importing-numpy-in-eclipse-pydev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!