Trent Mick wrote:
>
[snipped]
A couple of more questions, ;-)
> On Wed, Feb 07, 2001 at 11:55:20AM +0800, LUK ShunTim wrote:
> >
> > \begin{quote}
> >
> > ActivePython 2.0, build 202 (ActiveState Tool Corp.)
> > based on Python 2.0 (#8, Oct 19 2000, 11:30:05) [MSC 32 bit (Intel)] on
> > win32
> > Type "copyright", "credits" or "license" for more information.
> > >>> dir()
> > ['__builtins__', '__doc__', '__name__']
> > >>> dir(site)
> > Traceback (most recent call last):
> > File "<stdin>", line 1, in ?
> > NameError: There is no variable named 'site'
> > >>> import site
> > >>> dir(site)
> > ['_Printer', '__builtin__', '__builtins__', '__doc__', '__file__',
> > '__name__', '
> > _test', 'addpackage', 'addsitedir', 'encoding', 'here', 'makepath',
> > 'os', 'prefi
> > x', 'prefixes', 'sitedir', 'sitedirs', 'sys']
> > >>>
> >
> > \end{quote}
> >
> > Why doesn't the first dir(site) list the names from the site module?
>
> The imported site module (or more specifically an object named 'site'
> refering to the imported site module object) is not placed in the '__main__'
> namespace (this is the namespace that gets listed by default with "dir()")
>
> When you "import" a module, Python first checks it cache to see if the module
> has already been imported, only reloads it if necessary, and then sets up a
> reference in the current namespace. You can see the cache of loaded modules
> in sys.modules so...
>
> D:\>python
> ActivePython 2.0, build 202 (ActiveState Tool Corp.)
> based on Python 2.0 (#8, Oct 19 2000, 11:30:05) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> >>> dir()
> ['__builtins__', '__doc__', '__name__']
>
> So far there are no modules in the __main__ namespace.
>
> >>> import sys
> >>> dir()
> ['__builtins__', '__doc__', '__name__', 'sys']
>
> Now a ref to the sys module has been added to the __main__ namespace.
>
> >>> sys.modules
> {'win32api': <module 'win32api' from 'D:\apps\Python20\win32\win32api.pyd'>, 'os
> .path': <module 'ntpath' from 'd:\apps\python20\lib\ntpath.pyc'>, 'os': <module
> 'os' from 'd:\apps\python20\lib\os.pyc'>, 'exceptions': <module 'exceptions' (bu
> ilt-in)>, '__main__': <module '__main__' (built-in)>, 'ntpath': <module 'ntpath'
> from 'd:\apps\python20\lib\ntpath.pyc'>, 'nt': <module 'nt' (built-in)>, 'sys':
> <module 'sys' (built-in)>, '__builtin__': <module '__builtin__' (built-in)>, 's
> ite': <module 'site' from 'd:\apps\python20\lib\site.pyc'>, 'signal': <module 's
> ignal' (built-in)>, 'UserDict': <module 'UserDict' from 'd:\apps\python20\lib\us
> erdict.pyc'>, 'stat': <module 'stat' from 'd:\apps\python20\lib\stat.pyc'>}
>
> You can see that the 'sys' and 'site' modules (among others) have been
> loaded.
[snipped]
That means names in the modules that are imported by default when
invoking python is not automatically available to the user in the
interpreter environment, ie, the __main__ namespace I suppose. Is it the
case?
If it is so, is there any reason why that it should be implemented this
way? Surely these names are used very often and it makes sense not to
have to re-input them again.
And that means if I'd like to get, say, sys in the __main__ namespace at
start every time, I *have to* do it using a startup script.
Finally, in a non-interactive invoking of python, are names in these
import-by-default
modules available?
Thanks once again,
ST
--
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython