Some tools for developers


  • Nicolas M. Thiery: initial version
  • Vincent Delecroix (2012): improve import_statements
sage.misc.dev_tools.import_statements(*objects, **options)

Print import statements for the given objects.


  • *objects – a sequence of objects.
  • lazy – a boolean (default: False) Whether to print a lazy import statement.
  • verbose – a boolean (default: True) Whether to print information in case of ambiguity.


sage: import_statements(WeylGroup, lazy_attribute)
from sage.combinat.root_system.weyl_group import WeylGroup
from sage.misc.lazy_attribute import lazy_attribute

sage: import_statements(IntegerRing)
from sage.rings.integer_ring import IntegerRing

If lazy is True, then lazy_import() statements are displayed instead:

sage: import_statements(WeylGroup, lazy_attribute, lazy=True)
from sage.misc.lazy_import import lazy_import
lazy_import('sage.combinat.root_system.weyl_group', 'WeylGroup')
lazy_import('sage.misc.lazy_attribute', 'lazy_attribute')

In principle, the function should also work on object which are instances. In case of ambiguity, one or two warning lines are printed:

sage: import_statements(RDF)
from sage.rings.real_double import RDF

sage: import_statements(ZZ)
  ** Warning **: several names for that object: Z, ZZ
from sage.rings.integer_ring import Z

sage: import_statements(euler_phi)
from sage.rings.arith import euler_phi

sage: import_statements(x)
  ** Warning **: several modules for that object: sage.all_cmdline, sage.calculus.predefined
from sage.calculus.predefined import x

If you don’t like the warning you can disable them with the option verbose:

sage: import_statements(ZZ, verbose=False)
from sage.rings.integer_ring import Z

sage: import_statements(x, verbose=False)
from sage.calculus.predefined import x

If the object has several names, an other way to get the import statement you expect is to use a string instead of the object:

sage: import_statements(cached_function)
  ** Warning **: several names for that object: CachedFunction, cached_function
from sage.misc.cachefunc import CachedFunction

sage: import_statements('cached_function')
from sage.misc.cachefunc import cached_function
sage: import_statements('Z')
from sage.rings.integer_ring import Z

Specifying a string is also useful for objects that are not imported in the Sage interpreter namespace by default. In this case, an object with that name is looked up in all the modules that have been imported in this session:

sage: print_import_statement
Traceback (most recent call last):
NameError: name 'print_import_statement' is not defined

sage: import_statements("print_import_statement")
from sage.misc.dev_tools import print_import_statement

We test different object which have no appropriate answer:

sage: import_statements('my_tailor_is_rich')
Traceback (most recent call last):
ValueError: no import statement for my_tailor_is_rich
sage: import_statements(5)
Traceback (most recent call last):
ValueError: no import statement for 5

We test that it behaves well with lazy imported objects (trac ticket #14767):

sage: import_statements(NN)
from sage.rings.semirings.non_negative_integer_semiring import NN
sage: import_statements('NN')
from sage.rings.semirings.non_negative_integer_semiring import NN
sage.misc.dev_tools.module_names_cmp(x, y)

A comparison function for module names.

This function first compares the depth of the modules and then breaks ties by alphabetical order.

See also

This function is used in import_statements().


sage: from sage.misc.dev_tools import module_names_cmp
sage: l = ['sage.groups.perm_gps', 'sage.combinat', 'sage.all', 'sage.plot.plot3d']
sage: sorted(l, cmp=module_names_cmp)
['sage.all', 'sage.combinat', 'sage.groups.perm_gps', 'sage.plot.plot3d']
sage.misc.dev_tools.print_import_statement(module, name, lazy)

Print an import statement.


  • module – the name of a module
  • name – the name of the object to import
  • lazy – a boolean: whether to print a lazy import statement


sage: sage.misc.dev_tools.print_import_statement('sage.misc.dev_tools', 'print_import_statement', False)
from sage.misc.dev_tools import print_import_statement
sage: sage.misc.dev_tools.print_import_statement('sage.misc.dev_tools', 'print_import_statement', True)
lazy_import('sage.misc.dev_tools', 'print_import_statement')

Graphical profiling with runsnake


  • command – the command to be run as a string.


sage: runsnake("list(SymmetricGroup(3))")        # optional - runsnake

command is first preparsed (see preparse()):

sage: runsnake('for x in range(1,4): print x^2') # optional - runsnake

runsnake() requires the program runsnake. Due to non trivial dependencies (python-wxgtk, ...), installing it within the Sage distribution is unpractical. Hence, we recommend installing it with the system wide Python. On Ubuntu 10.10, this can be done with:

> sudo apt-get install python-profiler python-wxgtk2.8 python-setuptools
> sudo easy_install RunSnakeRun

See the runsnake website for instructions for other platforms.

runsnake() further assumes that the system wide Python is installed in /usr/bin/python.

Previous topic

Class inheritance graphs

Next topic

Simple profiling tool

This Page