On 04/29/2013 03: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



Yes, you need a separate import from any module that references numpy. Don't worry about performance, subsequent imports do not take noticeable time, since the module objects are cached.

Any time you want to use a symbol from another module, you have to get it somehow. into your own module. You do not have to get it from the import statement, but it's certainly the simplest way, and the way that's usually clearest.

Each module represents a namespace, and except for the global namespace which is handled specially, you have to describe which ones you want access to. It's a feature, not a limitation.

There are lots of modules automatically imported indirectly by the ones you use. A quick test on a local copy of Python 3.3 shows 51 modules imported before I write any code. I certainly wouldn't want all of them visible in my script's namespace.

> -- but then what is the point of having a startup file?
>

Presumably by startup file, you mean script. Without a script, Python wouldn't have any idea what code to run. You import a few modules, use functionality from them, and lots of things happen behind the scenes. Fortunately for all of us, Python doesn't throw all the symbols into one big pile.



--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to