Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98827 > unrolled thread

What is the right way to import a package?

Started byfl <rxjwg98@gmail.com>
First post2015-11-14 12:00 -0800
Last post2015-11-16 12:53 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  What is the right way to import a package? fl <rxjwg98@gmail.com> - 2015-11-14 12:00 -0800
    Re: What is the right way to import a package? Christian Gollwitzer <auriocus@gmx.de> - 2015-11-14 22:59 +0100
    Re: What is the right way to import a package? Grobu <snailcoder@retrosite.invalid> - 2015-11-16 12:53 +0100

#98827 — What is the right way to import a package?

Fromfl <rxjwg98@gmail.com>
Date2015-11-14 12:00 -0800
SubjectWhat is the right way to import a package?
Message-ID<8e325d88-6ad1-46e3-a1fb-ef8c28dc50a8@googlegroups.com>
Hi,

I want to use a code snippet found on-line. It has such content:

from numpy import *
dt = 0.1
# Initialization of state matrices
X = array([[0.0], [0.0], [0.1], [0.1]])

# Measurement matrices
Y = array([[X[0,0] + abs(randn(1)[0])], [X[1,0] + abs(randn(1)[0])]])



When the above content is inside a .py document and running, there will be
 an error:

---> 15 Y = array([[X[0,0] + abs(randn(1)[0])], [X[1,0] + abs(randn(1)[0])]])
     16 #Y = ([[X[0,0]], [X[1,0] + 0]])

NameError: name 'randn' is not defined 


But when I run the above line by line at the console (Canopy), there will be
no error for the above line.

My question is: 

The import and the following are wrong. 

X = array([[0.0], [0.0], [0.1], [0.1]])

It should be:

import numpy as np
...
Y = np.array([[X[0,0] + abs(np.randn(1)[0])], [X[1,0] + abs(np.randn(1)[0])]])

This looks like the code I once saw. But the file when running has such
 error:

---> 15 Y = np.array([[X[0,0] + abs(np.randn(1)[0])], [X[1,0] + abs(np.randn(1)[0])]])

AttributeError: 'module' object has no attribute 'randn'

When it is run line by line at the console, it has the same error.

It is strange that the same content has errors depends on inside a file, or
at CLI console.

What is missing I don't realize? Thanks,

[toc] | [next] | [standalone]


#98829

FromChristian Gollwitzer <auriocus@gmx.de>
Date2015-11-14 22:59 +0100
Message-ID<n28an5$2f8$1@dont-email.me>
In reply to#98827
Am 14.11.15 um 21:00 schrieb fl:
> from numpy import *
> dt = 0.1
> # Initialization of state matrices
> X = array([[0.0], [0.0], [0.1], [0.1]])
>
> # Measurement matrices
> Y = array([[X[0,0] + abs(randn(1)[0])], [X[1,0] + abs(randn(1)[0])]])
>
> When the above content is inside a .py document and running, there will be
>   an error:
>
> ---> 15 Y = array([[X[0,0] + abs(randn(1)[0])], [X[1,0] + abs(randn(1)[0])]])
>       16 #Y = ([[X[0,0]], [X[1,0] + 0]])
>
> NameError: name 'randn' is not defined
>
> But when I run the above line by line at the console (Canopy), there will be
> no error for the above line.

Maybe Canopy is doing a

	from pylab import *

for you?

	Christian

[toc] | [prev] | [next] | [standalone]


#98872

FromGrobu <snailcoder@retrosite.invalid>
Date2015-11-16 12:53 +0100
Message-ID<n2cfug$4ad$1@dont-email.me>
In reply to#98827
On 14/11/15 21:00, fl wrote:
> Hi,
>
> I want to use a code snippet found on-line. It has such content:
>
> from numpy import *
> dt = 0.1
> # Initialization of state matrices
> X = array([[0.0], [0.0], [0.1], [0.1]])
>
> # Measurement matrices
> Y = array([[X[0,0] + abs(randn(1)[0])], [X[1,0] + abs(randn(1)[0])]])
>
>
>
> When the above content is inside a .py document and running, there will be
>   an error:
>
> ---> 15 Y = array([[X[0,0] + abs(randn(1)[0])], [X[1,0] + abs(randn(1)[0])]])
>       16 #Y = ([[X[0,0]], [X[1,0] + 0]])
>
> NameError: name 'randn' is not defined
>
>
> But when I run the above line by line at the console (Canopy), there will be
> no error for the above line.
>
> My question is:
>
> The import and the following are wrong.
>
> X = array([[0.0], [0.0], [0.1], [0.1]])
>
> It should be:
>
> import numpy as np
> ...
> Y = np.array([[X[0,0] + abs(np.randn(1)[0])], [X[1,0] + abs(np.randn(1)[0])]])
>
> This looks like the code I once saw. But the file when running has such
>   error:
>
> ---> 15 Y = np.array([[X[0,0] + abs(np.randn(1)[0])], [X[1,0] + abs(np.randn(1)[0])]])
>
> AttributeError: 'module' object has no attribute 'randn'
>
> When it is run line by line at the console, it has the same error.
>
> It is strange that the same content has errors depends on inside a file, or
> at CLI console.
>
> What is missing I don't realize? Thanks,
>
>

You can try :
from numpy import *
from numpy.random import *

HTH,

- Grobu -

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web