Simple question about modules/importing

85 views
Skip to first unread message

Sam Magura

unread,
Jul 18, 2011, 5:44:44 PM7/18/11
to sy...@googlegroups.com
I need to test to see if an object is a sympy.plotting.plot.Plot - i.e. Plot object, but I'm running in to trouble. 

What I want to do:

>>> myPlot = Plot(0)
>>> isinstance(myPlot, something)
True

Issues I'm having:

>>> import sympy
>>> sympy.plot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'plot'
>>> sympy.Plot
<function Plot at 0x8c00f44>
>>> sympy.plotting
<module 'sympy.plotting' from 'sympy/plotting/__init__.pyc'>
>>> sympy.plotting.plot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'plot'
>>> import sympy.plotting.plot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "sympy/plotting/plot.py", line 10, in <module>
    raise ImportError("pyglet is required for plotting.\n visit http://www.pyglet.org/")
ImportError: pyglet is required for plotting.


Thanks.

Aaron Meurer

unread,
Jul 18, 2011, 11:02:40 PM7/18/11
to sy...@googlegroups.com
The easiest way to figure this out is to see what the type of the object is.

In [1]: a = Plot(0)

In [2]: a
Out[2]: [0]: 0, 'mode=cartesian'

In [3]: type(a)
Out[3]: <class 'sympy.plotting.plot.Plot'>

Here, we see that the class is in sympy.plotting.plot. Unfortunately,
it is not the same as the Plot used above, which is just a function:

In [6]: type(Plot)
Out[6]: <type 'function'>

So we import it as a different name to avoid overriding the Plot function:

In [8]: from sympy.plotting.plot import Plot as PlotClass

In [9]: PlotClass
Out[9]: <class 'sympy.plotting.plot.Plot'>

In [11]: isinstance(a, PlotClass)
Out[11]: True

By the way, this error is misleading because there is a bare except
converting all errors into this one. See
https://github.com/sympy/sympy/pull/497.

Aaron Meurer

>
> Thanks.
>

Sam Magura

unread,
Jul 18, 2011, 11:35:55 PM7/18/11
to sy...@googlegroups.com
Thanks for your help Aaron.

Now that I tried importing sympy.plotting.plot again, it worked.  Apparently the error about pyglet was specific to the directory I was in earlier (it has a symlink to sympy.)  I just pulled from sympy master and still got the same error from my symlink directory, so I assume that that means the error message was correct.

Aaron Meurer

unread,
Jul 18, 2011, 11:46:31 PM7/18/11
to sy...@googlegroups.com
That pull request I linked to hasn't been merged yet, so the behavior
in master would still be the same.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/PM48NaNVcUIJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

Sam Magura

unread,
Jul 18, 2011, 11:54:32 PM7/18/11
to sy...@googlegroups.com
It looks like Chris Smith merged it
Reply all
Reply to author
Forward
0 new messages