warning: from sympy import * is deprecated

93 views
Skip to first unread message

Volker Weißmann

unread,
Jul 3, 2020, 10:53:27 AM7/3/20
to sympy
Hello,

I have library that is organized as follows:

mymod/__init__.py:
from .base import *

mymod/base.py:
import sympy
from .modA import *
from .modB import *

mymod/modA.py:
from .base import *
var = sympy.numbers.Zero

mymod/modB.py:
from .base import *
# other code


When I run python -c "import mymod", I get the warning:

/home/volker/.local/lib/python3.8/site-packages/sympy/__init__.py:672: SymPyDeprecationWarning:

importing sympy.core.numbers with 'from sympy import *' has been
deprecated since SymPy 1.6. Use import sympy.core.numbers instead. See
https://github.com/sympy/sympy/issues/18245 for more info.

  self.Warn(

Should I be worried?  Should I change my import scheme?

Oscar Benjamin

unread,
Jul 3, 2020, 11:28:27 AM7/3/20
to sympy
Hi Volker,

The problem is that you are using sympy.numbers.Zero. There is no
sympy.numbers module. Rather there is a sympy.core.numbers module
which is where Zero is defined. In SymPy 1.5 the main
sympy/__init__.py used to do
from .core import *
which resulted in the sympy.core.numbers module being imported into
the __init__.py as numbers which then made it accessible as
sympy.numbers. This was unintentional and was a result of sympy
internally using import * without consistently using __all__.

In sympy 1.6 this is fixed and the old modules that were accidentally
imported are still there but give a deprecation warning if you try to
use them. They will be removed in future though so sympy.numbers.Zero
will end up giving an error.

There are several ways to get Zero. To get it from the module where it
is defined you can use sympy.core.numbers.Zero or
from sympy.core.numbers import Zero
However in general users are encouraged not to depend on exactly which
module sympy objects are defined in except where the documentation
advises importing in a particular way. The intended way to get Zero
is:

from sympy import S
Zero = S.Zero # Or just use S.Zero wherever you would use Zero

Another way is just Integer(0) or S(0) because any sympy zero Integer
will return S.Zero.

The S object gives access to a number of sympy objects like S.pi,
S.One, S.NaN etc. You can also use it as a shorthand for sympify as in
S(1)/3 which gives a sympy Rational rather than a python float as 1/3
would.
https://docs.sympy.org/latest/modules/core.html#s


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 sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/147c76c5-f166-434e-8c08-2bb2072b530co%40googlegroups.com.

Volker Weißmann

unread,
Jul 4, 2020, 11:05:29 AM7/4/20
to sy...@googlegroups.com
Hi Oscar,


using sympy.S.Zero does silence the warning, but isinstance(somevar,
sympy.S.Zero) does not work, because sympy.S.Zero is not the class but
an object. How do I get the class Zero?


Greetings

Volker

Oscar Benjamin

unread,
Jul 4, 2020, 11:15:50 AM7/4/20
to sympy
On Sat, 4 Jul 2020 at 16:05, Volker Weißmann
<volker.we...@gmail.com> wrote:
>
> using sympy.S.Zero does silence the warning, but isinstance(somevar,
> sympy.S.Zero) does not work, because sympy.S.Zero is not the class but
> an object. How do I get the class Zero?

You can do `type(S.Zero)` to get the class. Normally though you can
test for an instance using `is`:

x is S.Zero

Because Zero is a Singleton class it only has one instance.


Oscar

Volker Weißmann

unread,
Jul 4, 2020, 12:47:09 PM7/4/20
to sy...@googlegroups.com
Thank you, type(sympy.S.Zero)  seems to work.
Reply all
Reply to author
Forward
0 new messages