__builtin__.open on windows NT

Fredrik Lundh effbot at telia.com
Thu Apr 6 12:59:21 EDT 2000


Thomas Wouters <thomas at xs4all.net> wrote:
> > i'm new to python, so be prepared...i'm having trouble with open() on
> > windows NT.  my script uses the os library and it appears that open()
> > in my script is conflicting with an open() function in the os lib.  so
> > i tried to use __builtin__.open(), but i get this error "NameError:
> > __builtin__"
>
> Well, if you have that conflict, you are doing 'from os import *'.
> The easy solution is 'dont do that then'. 'from <module> import *' can be
> very bad for your health, for exactly the reason you specify. My advice
(and
> probably that of most people here ;) would be to change the import into
just
> 'import os' and fix the places where you actually use os.
>
> If you *really* want to use __builtins__, you can: you just have to import
> it first ! :-) Remember to do that with 'import __builtins__', or it might
> overwrite some of os's variables...

ehem.  the module is called __builtin__, while __builtins__ is a
magic attribute which may or may not exist.

if you really need to refer to the module containing the standard
builtins, do:

    import __builtin__
    fp = __builtin__.open(...)

the best solution is to only use "from import" if you know exactly
if you're doing (or in other words, if you use "from import" and don't
understand what happens, don't use it).  also see:

    http://www.pythonware.com/people/fredrik/fyi/fyi06.htm

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list