1

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?

codeKiller
  • 5,493
  • 17
  • 60
  • 115
  • As you may already know, the script should only work with the second option. What do you mean with "the script works if I run it"? You mean inside Spyder, or if you run it from outside? Maybe Spyder is using another version of the interpreter, e.g. Python 3? Also, have you installed numpy by standard means? If not, maybe you have to tweak the PYTHONPATH in the "Tools" menu. – jdehesa May 06 '14 at 10:30
  • let's see....the script will also work with option 3 doing `np.array...etc`, (which I have tested and is not working) now, Spyder comes with the package Python(x,y) so everything (python 2.7, numpy, matplotlib, etc) is installed. When I mentioned that the script works I wanted to mean inside Spyder by clicking "Run", so, even with more than 30 errors when compiling, it works from the beginning to the end without raising any error, even when walking over the lines marked as "Error" in the picture above. That's weird... – codeKiller May 06 '14 at 11:15

2 Answers2

4

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.

jdehesa
  • 58,456
  • 7
  • 77
  • 121
  • Thanks for your answer javidcf, I will make a test with the simple script that you suggested. However, as I am coding and coding, each time I use numpy i get a new error...so I am tired to see more than 70 errors now each time I try to "compile"....but ok – codeKiller May 06 '14 at 12:49
  • 2
    @user2919052 If you grow really tired of it the simplest solution would be to put `# pylint: disable=E0602` or `# pylint: disable=E1101` to disable these errors in the analysis; however, note that this will disable all of the errors of this type in the script. – jdehesa May 06 '14 at 12:54
  • 2
    The issue jdehesa is referencing has been solved and closed. Update astroid to the latest version (as of now 1.2.1) and pylint will not detect this false positive anymore. Just to clarify, despite all the comments in the pylint issue (and also here), this had nothing to do with dynamic definition of members. The issue was related to the use of absolute_import imported from `__future__` – zeehio Nov 19 '14 at 20:30
0
import numpy as np

then use

QR10 = np.array(QR10,dtype=float)  # instead of numpy.array
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
  • `import numpy as np` and after that `QR10 = np.array(QR10,dtype=float)` also tried and getting error....Is it something with Spyder?? – codeKiller May 06 '14 at 09:17
  • Hummm... have you tried restarting the kernel, and starting over? – Reblochon Masque May 06 '14 at 09:19
  • hehehe... this is frustrating, eh? There must be a numpy somewhere hidden in your code, your mission is to find it :) – Reblochon Masque May 06 '14 at 09:22
  • mm that's interesting...what do you mean with "numpy somewhere hidden in your code"?? you mean the word "numpy" or...what should I exactly look for?? – codeKiller May 06 '14 at 09:24
  • yes, this is what i meant. If you **import numpy as np**, numpy should normally appear only in the import line; all other instances should be **np**; maybe it is a good idea to verify that you don't have a sneaky numpy hiding somewhere and convert it to np... :) with all the tries you've done, you may have a bit of clean up to do. – Reblochon Masque May 06 '14 at 09:31
  • you are right, however, I have been investigating my code....I am unsing the formula `import numpy`and then using it as `numpy.array` or whatever...and still getting more than 30 errors same as above... – codeKiller May 06 '14 at 09:39