python Spyder not importing numpy

泪湿孤枕 提交于 2021-01-28 17:51:36

问题


I am writing a script using python Spyder 2.2.5 with Windows 7, python 2.7

At the very beginning I have tried all the import ways:

from numpy import *

or

import numpy

and also

import numpy as np

And, for each an every line where I use numpy I am getting an error when compiling

QR10 = numpy.array(QR10,dtype=float)
QR20 = numpy.array(QR20,dtype=float)
QR11 = numpy.array(QR11,dtype=float)
QR21 = numpy.array(QR21,dtype=float)

enter image description here

However, even with this 30 errors, the script works if I run it....

Any help about this?


回答1:


Python cannot actually be compiled. Spyder performs just a static code analysis using Pylint. Depending on the version of Pylint that is being used, it could be a bug or an undetectable case for it.

For example, the import statement (or the path that gets to it) could be within a conditional block, which cannot be resolved until runtime. Given that you are using Spyder, it could also be that you put your import statement directly on the console, or in a separate file, and then use the imported module from the script.

You may try to see if you receive the same error with a script like the following:

import numpy

QR10 = [1, 2, 3]
QR20 = [1, 2, 3]
QR11 = [1, 2, 3]
QR21 = [1, 2, 3]

QR10 = numpy.array(QR10,dtype=float)
QR20 = numpy.array(QR20,dtype=float)
QR11 = numpy.array(QR11,dtype=float)
QR21 = numpy.array(QR21,dtype=float)

You should not see the E0602 here. Funny enough, however, you may receive [E1101] Module 'numpy' has no 'array' member, because it turns out that numpy does some dynamic definition of members, so Pylint cannot know about it (as you may see here) of a bug that has actually been solved already.

The moral of the story is that Pylint errors shouldn't keep you awake at night. It's good to see the report, but if you are sure that your code makes sense and it runs just right, you may just ignore them - although trying to know why it is giving an error is always a good exercise.




回答2:


import numpy as np

then use

QR10 = np.array(QR10,dtype=float)  # instead of numpy.array


来源:https://stackoverflow.com/questions/23490113/python-spyder-not-importing-numpy

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