Mystery of module bindings!

Ned Batchelder ned at nedbatchelder.com
Mon Apr 29 15:50:10 EDT 2013


On 4/29/2013 3: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?

The startup file is only for interactive use.  Python programs in files 
shouldn't depend on a startup file, as it will limit their portability.  
In an interactive session, it's helpful to save some typing, but you can 
use explicit imports in programs, as they only need to be typed once, 
and will be used many more times.

--Ned.

>
> -- PeterR




More information about the Python-list mailing list