[Tutor] Danny's __std__ module

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed May 28 17:15:57 2003


On Wed, 28 May 2003 tpc@csua.berkeley.edu wrote:


> hi Danny, I was curious: when you made this module did you intend for it
> to be called only when you name a file after a standard module, or for
> general everyday use?

Hello!

The first case: when someone is forced to name a program with the same
name as something from the Standard Library, __std__ is a way to do it.
In almost all cases, __std__ is completely unnecessary, so I don't expect
to see it in widespread use.  *grin*



> I ask because I am writing a script and when I called your module in my
> script and then tried to run it I got:
>
> [tpc@nike htdocs]# ./text2decimalascii.py hello world
> Traceback (most recent call last):
>   File "./text2decimalascii.py", line 2, in ?
>     from __std__ import sys
> ImportError: cannot import name sys


??!  Yikes!  I did expect that to work.  That's a bug on the part of the
__std__ module.  ... ah.  The 'sys' module seems to be one of the core
modules in Python, and doesn't live in the same place as the rest of the
Standard Library.  In fact, it appears to be statically compiled into
Python itself.  So you hit on one of the few special cases.  *grin* I
wonder if there are others...

We can see that 'sys' is hardcoded by doing a 'help(sys)'; unlike the
other standard Python modules, 'sys' doesn't have a __file__ attribute.



Ok, I think I can fix __std__ to make it handle 'sys' as a special case.
Ok, done.  Can you try it out?  If you can replace the file
__std__/__init__.py with:



###
import distutils.sysconfig


"""Note: it is important to do an 'import sys' here!  'sys' doesn't
really live in distutils.sysconfig.get_python_lib()--- it's statically
compiled into Python.  If we want:

    from __std__ import sys

to work, we need to have 'sys' here in our namespace.

Thanks to tpc@csua.berkeley.edu for pointing this out.
"""
import sys



__path__ = [distutils.sysconfig.get_python_lib(standard_lib=1)]

__all__ = []
###



and reinstall __std__, the 'from __std__ import sys' should then work.
If it looks good with you, I'll update the package with the fix.



Thanks again!