Compilation difference in Spyder IDE and Python intrepeter

1.4k views Asked by At

This problem may seem a little strange, but a while (about 1-2 weeks) ago I wrote a little Python script which I tested and everything worked just fine. Today when I was taking lines from this latter script, the lines would run without any error in the Spyder IDE Python console, but when I try to put those same line in a new .py file, Spyder gives me errors!

So I tried to compile the old script again, then I got errors!

A few examples to maybe clear things up:

  1. I load and image using PIL Image: im = Image.open("test.jpg")
    then in the Spyder console I can do: im.layers which gives me the number of color channels. Even though this attribute doesn't exist in PIL Image docs!
    But using this same attribute in a python file would give an error!
  2. Using: a = array( [ [ 1, 2, 3], [4, 5, 6], [...] ] ) I can create a 2D array (or matrix). This is possible through Spyder, but not regular Python interpreter (which leads to NameError: global name 'array' is not defined )!

And a few more examples like these.

Could anyone help me understand what is going on, knowing that I'm sort of a Python noob?

Python version: 2.7.6 | GCC 4.8.2 | Spyder 2.2.5

1

There are 1 answers

5
petem On BEST ANSWER

Spyder 2.2.5 is an older version (the last version is 2.3.4). When it is started automatically imports numpy and matplotlib. The regular Python interpreter needs an explicit import numpy as np in order to define an array, A=np.array([[1,2,3], [4,5,6]]).