In Python 2 builtins was called called __builtin__. That's why it's in
the compatibility module (not to be confused with __builtins__, which
is a different thing, hence the rename). You could just as well write

try:
    # Python 3
    from builtins import type
except ImportError:
    # Python 2
    from __builtin__ import type

but the whole point of the SymPy compatibility module is to isolate
such 2/3 compatibility code into one place. So it's better to do

from sympy.core.compatibility import builtins
builtins.type

There's also another possible level of confusion here which is that if
you have the 'future' package installed in your Python 2 environment,
it automatically adds a builtins module to the standard library. So in
that case, 'from builtins import type' will work. But it shouldn't be
relied on for SymPy, because we do not have 'future' as a dependency
(and do not want to have it as one).

Aaron Meurer

On Thu, May 9, 2019 at 2:56 PM Chris Smith <[email protected]> wrote:
>
> Hmm - yes, that works for me, too. But in Py3 you can do 'from builtins 
> import type' -- is the only way to do that in 2 is to use monkey patching?
>
> /c
>
> On Thursday, May 9, 2019 at 2:36:21 PM UTC-5, Aaron Meurer wrote:
>>
>> This works for me:
>>
>> Python 2.7.14 | packaged by conda-forge | (default, Dec  9 2017, 16:31:12)
>> [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> from sympy.core.compatibility import builtins
>> >>> builtins.type
>> <type 'type'>
>>
>> Aaron Meurer
>>
>> On Thu, May 9, 2019 at 2:02 AM Chris Smith <[email protected]> wrote:
>> >
>> > builtins is in core.compatibility and it doesn't seem to work for me.
>> >
>> > The issue will go away when we drop Py 2.7...or sooner if someone can 
>> > figure out what's wrong.
>> >
>> > On Wednesday, May 8, 2019 at 5:02:05 PM UTC-5, Aaron Meurer wrote:
>> >>
>> >> On Wed, May 8, 2019 at 3:48 PM Oscar Benjamin
>> >> <[email protected]> wrote:
>> >> >
>> >> > On Wed, 8 May 2019 at 22:38, Chris Smith <[email protected]> wrote:
>> >> > >
>> >> > > None of them work
>> >> >
>> >> > That's what I get for not trying them out!
>> >> >
>> >> > > >>> import sympy.core.compatibility.builtins as builtins
>> >> > > Traceback (most recent call last):
>> >> > >   File "<stdin>", line 1, in <module>
>> >> > > ImportError: No module named builtins
>> >> > >
>> >> > > >>> from sympy.core.compatibility.builtins import type
>> >> > > Traceback (most recent call last):
>> >> > >   File "<stdin>", line 1, in <module>
>> >> > > ImportError: No module named builtins
>> >> >
>> >> > Yes that's because there is no builtins module in the compatibility
>> >> > package. In fact compatibility is a module not a package.
>> >> >
>> >> > > >>> sys.modules['builtins']=__builtin__
>> >> > > Traceback (most recent call last):
>> >> > >   File "<stdin>", line 1, in <module>
>> >> > > NameError: name '__builtin__' is not defined
>> >> >
>> >> > This will work if you have imported __builtin__ first:
>> >> >
>> >> > >>> import __builtin__
>> >> > >>> import sys
>> >> > >>> sys.modules['builtins'] = __builtin__
>> >> > >>> import builtins
>> >>
>> >> What is this for? We shouldn't be monkey-patching sys.modules in library 
>> >> code.
>> >>
>> >> Aaron Meurer
>> >>
>> >> >
>> >> > --
>> >> > Oscar
>> >> >
>> >> > --
>> >> > You received this message because you are subscribed to the Google 
>> >> > Groups "sympy" group.
>> >> > To unsubscribe from this group and stop receiving emails from it, send 
>> >> > an email to [email protected].
>> >> > To post to this group, send email to [email protected].
>> >> > Visit this group at https://groups.google.com/group/sympy.
>> >> > To view this discussion on the web visit 
>> >> > https://groups.google.com/d/msgid/sympy/CAHVvXxQ59yzRzns5MHiqNmZz09Tx82B4aF_CnvidTwMGPA9E6Q%40mail.gmail.com.
>> >> > For more options, visit https://groups.google.com/d/optout.
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "sympy" group.
>> > To unsubscribe from this group and stop receiving emails from it, send an 
>> > email to [email protected].
>> > To post to this group, send email to [email protected].
>> > Visit this group at https://groups.google.com/group/sympy.
>> > To view this discussion on the web visit 
>> > https://groups.google.com/d/msgid/sympy/bb44f6d2-8d32-410b-97e7-5bd4e2409a2a%40googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/13957bcc-ec40-4f21-a2c0-05576db5db53%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6J3ZqixzTy4J%3DOej5pmDfmME6FUYuwo%2BfpXYgJ7VJ5ynQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to