Re: [spyder] arrays in Spyder

742 views
Skip to first unread message

David Nicholls

unread,
Dec 30, 2012, 6:12:38 AM12/30/12
to spyd...@googlegroups.com
I get the error message from Enthought iPython whether or not I'm using Spyder, unless I prefix the commands with 'from numpy import array'

DN

On 30/12/2012, at 2:36 AM, kar...@charter.net wrote:

> Can't create arrays consistently in Spyder (v2.1.11 within Python(x,y)). A simple program like:
> lst = [[1,2,3],[4,5,6],[11,12,13]]
> x = array(lst,int) #or x = array(lst)
> print x
> gives the error: NameError: name 'array' is not defined
> At other times, it correctly prints out the array. I'm not sure how my settings are off that I would get so frustratingly inconsistent output. Or is it rather a problem with arrays in Python or Spyder itself. Thanks for any help.
>
> Charles McCall
>
> --
> You received this message because you are subscribed to the Google Groups "spyder" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/spyderlib/-/aACCbYYNLbYJ.
> To post to this group, send email to spyd...@googlegroups.com.
> To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.

Pierre Raybaut

unread,
Dec 30, 2012, 11:09:28 AM12/30/12
to spyd...@googlegroups.com
Hi,

The fact is that 'array' is not a Python builtin function, in other words you have to import the symbol before using it with a statement like "from numpy import array". FYI, according to the NumPy official coding guidelines, you should actually use something like "import numpy as np" and then "np.array". It is a compact way of importing and using NumPy without having to import every single symbol explicitly. 

Sometimes, like in Spyder's Python interpreter with the default "scientific startup script", all NumPy symbols are already imported as if they were builtins. This is not a good practice at all, but it's simply more convenient when playing with Python interactively in an interpreter. Maybe this is why you got the (wrong) impression that 'array' had to be available in any Python script without having to do anything first. 

Cheers,
-Pierre 


Le 30 déc. 2012 à 11:17, "kar...@charter.net" <kar...@charter.net> a écrit :

Carlos Córdoba

unread,
Dec 31, 2012, 6:07:50 PM12/31/12
to spyd...@googlegroups.com
This depends in one key fact: if you are using the pylab option in IPython. If so, then you can expect using just 'array' to work. If not, you have to import it as Pierre or David described.

I know this is confusing, especially for users coming from Matlab. The Pylab option was meant to ease this transistion, because it preloads numpy and matplotlib so that they can be used as in Matlab.

Cheers,
Carlos

2012/12/29 <kar...@charter.net>
Reply all
Reply to author
Forward
0 new messages