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


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

Re: Mystery of module bindings!

Started byEthan Furman <ethan@stoneleaf.us>
First post2013-04-29 12:53 -0700
Last post2013-04-29 12:53 -0700
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Mystery of module bindings! Ethan Furman <ethan@stoneleaf.us> - 2013-04-29 12:53 -0700

#44523 — Re: Mystery of module bindings!

FromEthan Furman <ethan@stoneleaf.us>
Date2013-04-29 12:53 -0700
SubjectRe: Mystery of module bindings!
Message-ID<mailman.1165.1367268102.3114.python-list@python.org>
On 04/29/2013 12:30 PM, Peter Rowat wrote:
> This must be a trivial question:
>
> I have "import numpy as np" in the python startup file.
>
> A file called mod1.py contains "def myfn..."
>   and inside myfn there is a call to, say, "np.convolve".
>
> Interactively:
>> python
> .... (numpy imported as np)
>
>> import mod1
>>
>> mod1.myfn(...)
>
> Error: global name "np" is not known.
> =======
> Why is "np" not known to functions in an imported module ?
> =======
>
> I can fix this by including "import numpy as np" in any module that uses numpy
> functions -- but then what is the point of having a startup file?

That "fix" is indeed the way things are done.  Modules only know about what they import or create*.

The point of the startup file is to have things ready for interactive investigation -- if you use numpy *alot* then 
having it already there beats the heck out of typing `import numpy as np` every time you start the interpreter.

--
~Ethan~

*Unless you start playing with injection and stuff.

[toc] | [standalone]


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


csiph-web