On Sun, Sep 23, 2012 at 1:02 PM, Stefan Krastanov <[email protected]> wrote: > There are two pieces of logic in the plotting module that require the > import of matplotlib and they both can be moved inside functions. > > 1) default_backend - at the moment the default_backend is decided on > import. This can be moved to the initialization of a Plot object. > > 2) matplotlib_backend - this is rather trivial - we move the import > inside the class definition
Moving it inside the class definition will have no effect. All code inside class definitions is run when the class is defined, so the module will still be imported when sympy is. It needs to be inside a function definition for the import to be deferred. For example: In [1]: import sys In [2]: sys.modules['sympy'] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-2-04cb623d31a7> in <module>() ----> 1 sys.modules['sympy'] KeyError: 'sympy' In [3]: class Test(object): ...: import sympy ...: In [4]: sys.modules['sympy'] Out[4]: <module 'sympy' from '/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/__init__.pyc'> But In [1]: import sys In [2]: sys.modules['sympy'] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-2-04cb623d31a7> in <module>() ----> 1 sys.modules['sympy'] KeyError: 'sympy' In [3]: class Test(object): ...: def __init__(self): ...: import sympy ...: In [4]: sys.modules['sympy'] --------------------------------------------------------------------------- KeyError Traceback (most recent call last) <ipython-input-4-04cb623d31a7> in <module>() ----> 1 sys.modules['sympy'] KeyError: 'sympy' In [5]: Test() Out[5]: <__main__.Test at 0x102b30110> In [6]: sys.modules['sympy'] Out[6]: <module 'sympy' from '/Users/aaronmeurer/Documents/Python/sympy/sympy/sympy/__init__.pyc'> Actually, unless you use class methods, just putting it __init__ should work. Making numpy not be imported is much more tricky, since the implicit plotting stuff also uses it. Aaron Meurer > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/sympy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
