[SciPy-user] Namespace Questions

Bill Alexander scipy-user@scipy.net
19 Feb 2003 22:07:09 -0800


Chalk these up to the "I want it like Matlab" category...  My apologies
for the length.

What I'd like to do is invoke scripting commands from an interactive
python session from the global namespace.  In Matlab, all m-file scripts
are members of and operate within the invoking namespace, whereas
functions create a private interior namespace for operation.  I want to
be able to mimic the no-namespace-change scripts of Matlab from within
the Python interactive shell.

The only thing I can figure to do is to use import.  However, this
creates a namespace for operation, and seems to restrict access to
objects not a direct member of that namespace.  I've looked for some
kind of namespace override command (sort of like a "chdir" or debugger
"up" and "down" for namespaces), but don't see anything applicable.

Here's a concrete example - if I can get this to work, I can get
scripting to do what I want.

First, from the command line to ensure it should work:
>>> from scipy import *
>>> x = randn(5)
>>> cos(x)
[ 0.82844182  0.70265662  0.55083457  0.58898356  0.9564757 ]

OK - works great.  Now, let's pretend that this was something more
significant, and that I'd like to invoke it as a script from within the
python shell.  Edit a file foo.py:

######
# foo.py
x = randn(5)
print cos(x)
#######

Now, restart the python interpreter and try this:
>>> from scipy import *
>>> from foo import *
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "foo.py", line 1, in ?
    x = randn(5)
NameError: name 'randn' is not defined

You can test variations of this with varying degrees of success.

Let me discuss one solution that won't work: importing the required
modules at the top of this module.  That is, to fix the above problem, I
could of course just issue a "from scipy import *" directive at the top
of the file.  I don't want this for two reasons: one, I will be manually
setting up the global namespace with desired features, including
functional and data objects, which I need to be imported into the script
(these scripts are going to be complicated and VERY quickly coded - I
don't have time for a detailed class design until I know what I'm doing,
which I won't know until I experiment with some data and ideas, and so
on...); and two, if I do make this a module with it's own namespace, I
don't want that namespace cluttered with the entire scipy set (I find
this to be very frustrating when dealing with the existing code modules
- the namespaces are overlapped and cluttered and it's hard to see what
is actually unique to the separate modules).

It doesn't make sense to me that modules which descend from the global
namespace don't automatically have access to objects in the global
space.  I understand about needing to specify write permission to global
objects with the global directive, but what about read-only invocation
of functional or class objects?  

Anyone got an idea or suggestion?  I have a feeling I just don't
understand Python's model yet.

Thanks in advance!

- Bill Alexander