Important

This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.

If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).

Module: core.magics.namespace

Implementation of namespace-related magic functions.

1 Class

class IPython.core.magics.namespace.NamespaceMagics(**kwargs: Any)

Bases: Magics

Magics to manage various aspects of the user’s namespace.

These include listing variables, introspecting into them, etc.

pdef(parameter_s='', namespaces=None)

Print the call signature for any callable object.

If the object is a class, print the constructor information.

Examples

In [3]: %pdef urllib.urlopen
urllib.urlopen(url, data=None, proxies=None)
pdoc(parameter_s='', namespaces=None)

Print the docstring for an object.

If the given object is a class, it will print both the class and the constructor docstrings.

pfile(parameter_s='', namespaces=None)

Print (or run through pager) the file where an object is defined.

The file opens at the line where the object definition begins. IPython will honor the environment variable PAGER if set, and otherwise will do its best to print the file in a convenient form.

If the given argument is not an object currently defined, IPython will try to interpret it as a filename (automatically adding a .py extension if needed). You can thus use %pfile as a syntax highlighting code viewer.

pinfo(parameter_s='', namespaces=None)

Provide detailed information about an object.

‘%pinfo object’ is just a synonym for object? or ?object.

pinfo2(parameter_s='', namespaces=None)

Provide extra detailed information about an object.

‘%pinfo2 object’ is just a synonym for object?? or ??object.

psearch(parameter_s='')

Search for object in namespaces by wildcard.

%psearch [options] PATTERN [OBJECT TYPE]

Note: ? can be used as a synonym for %psearch, at the beginning or at the end: both a*? and ?a* are equivalent to ‘%psearch a*’. Still, the rest of the command line must be unchanged (options come first), so for example the following forms are equivalent

%psearch -i a* function -i a* function? ?-i a* function

Parameters:
  • PATTERN

  • its (where PATTERN is a string containing * as a wildcard similar to)

  • the (written string. By adding a type here only objects matching)

  • not (search path. By default objects starting with a single _ are)

  • matched

  • single (many IPython generated objects have a)

  • is (ex. StringType)

  • objects (also done on the attributes of objects and not only on the)

  • module. (in a)

  • TYPE] ([OBJECT)

  • is

  • type (given in lowercase without the ending)

  • is

  • the

  • all (given type are matched. Using all here makes the pattern match)

  • types (this is the default)

Options:

-a: makes the pattern match even objects whose names start with a single underscore. These names are normally omitted from the search.

-i/-c: make the pattern case insensitive/sensitive. If neither of these options are given, the default is read from your configuration file, with the option InteractiveShell.wildcards_case_sensitive. If this option is not specified in your configuration file, IPython’s internal default is to do a case sensitive search.

-e/-s NAMESPACE: exclude/search a given namespace. The pattern you specify can be searched in any of the following namespaces: ‘builtin’, ‘user’, ‘user_global’,’internal’, ‘alias’, where ‘builtin’ and ‘user’ are the search defaults. Note that you should not use quotes when specifying namespaces.

-l: List all available object types for object matching. This function can be used without arguments.

‘Builtin’ contains the python module builtin, ‘user’ contains all user data, ‘alias’ only contain the shell aliases and no python objects, ‘internal’ contains objects used by IPython. The ‘user_global’ namespace is only used by embedded IPython instances, and it contains module-level globals. You can add namespaces to the search with -s or exclude them with -e (these options can be given more than once).

Examples

%psearch a*            -> objects beginning with an a
%psearch -e builtin a* -> objects NOT in the builtin space starting in a
%psearch a* function   -> all functions beginning with an a
%psearch re.e*         -> objects beginning with an e in module re
%psearch r*.e*         -> objects that start with e in modules starting in r
%psearch r*.* string   -> all strings in modules beginning with r

Case sensitive search:

%psearch -c a*         list all object beginning with lower case a

Show objects beginning with a single _:

%psearch -a _*         list objects beginning with a single underscore

List available objects:

%psearch -l            list all available object types
psource(parameter_s='', namespaces=None)

Print (or run through pager) the source code for an object.

reset(parameter_s='')

Resets the namespace by removing all names defined by the user, if called without arguments, or by removing some types of objects, such as everything currently in IPython’s In[] and Out[] containers (see the parameters for details).

Parameters:
  • -f – force reset without asking for confirmation.

  • -s – ‘Soft’ reset: Only clears your namespace, leaving history intact. References to objects may be kept. By default (without this option), we do a ‘hard’ reset, giving you a new session and removing all references to objects from the current session.

  • --aggressive – Try to aggressively remove modules from sys.modules ; this may allow you to reimport Python modules that have been updated and pick up changes, but can have unintended consequences.

  • in – reset input history

  • out – reset output history

  • dhist – reset directory history

  • array – reset only variables that are NumPy arrays

See also

reset_selective

invoked as %reset_selective

Examples

In [6]: a = 1

In [7]: a
Out[7]: 1

In [8]: 'a' in get_ipython().user_ns
Out[8]: True

In [9]: %reset -f

In [1]: 'a' in get_ipython().user_ns
Out[1]: False

In [2]: %reset -f in
Flushing input history

In [3]: %reset -f dhist in
Flushing directory history
Flushing input history

Notes

Calling this magic from clients that do not implement standard input, such as the ipython notebook interface, will reset the namespace without confirmation.

reset_selective(parameter_s='')

Resets the namespace by removing names defined by the user.

Input/Output history are left around in case you need them.

%reset_selective [-f] regex

No action is taken if regex is not included

Options

-f : force reset without asking for confirmation.

See also

reset

invoked as %reset

Examples

We first fully reset the namespace so your output looks identical to this example for pedagogical reasons; in practice you do not need a full reset:

In [1]: %reset -f

Now, with a clean namespace we can make a few variables and use %reset_selective to only delete names that match our regexp:

In [2]: a=1; b=2; c=3; b1m=4; b2m=5; b3m=6; b4m=7; b2s=8

In [3]: who_ls
Out[3]: ['a', 'b', 'b1m', 'b2m', 'b2s', 'b3m', 'b4m', 'c']

In [4]: %reset_selective -f b[2-3]m

In [5]: who_ls
Out[5]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']

In [6]: %reset_selective -f d

In [7]: who_ls
Out[7]: ['a', 'b', 'b1m', 'b2s', 'b4m', 'c']

In [8]: %reset_selective -f c

In [9]: who_ls
Out[9]: ['a', 'b', 'b1m', 'b2s', 'b4m']

In [10]: %reset_selective -f b

In [11]: who_ls
Out[11]: ['a']

Notes

Calling this magic from clients that do not implement standard input, such as the ipython notebook interface, will reset the namespace without confirmation.

who(parameter_s='')

Print all interactive variables, with some minimal formatting.

If any arguments are given, only variables whose type matches one of these are printed. For example:

%who function str

will only list functions and strings, excluding all other types of variables. To find the proper type names, simply use type(var) at a command line to see how python prints type names. For example:

In [1]: type('hello')\
Out[1]: <type 'str'>

indicates that the type name for strings is ‘str’.

%who always excludes executed names loaded through your configuration file and things which are internal to IPython.

This is deliberate, as typically you may load many modules and the purpose of %who is to show you only what you’ve manually defined.

Examples

Define two variables and list them with who:

In [1]: alpha = 123

In [2]: beta = 'test'

In [3]: %who
alpha   beta

In [4]: %who int
alpha

In [5]: %who str
beta
who_ls(parameter_s='')

Return a sorted list of all interactive variables.

If arguments are given, only variables of types matching these arguments are returned.

Examples

Define two variables and list them with who_ls:

In [1]: alpha = 123

In [2]: beta = 'test'

In [3]: %who_ls
Out[3]: ['alpha', 'beta']

In [4]: %who_ls int
Out[4]: ['alpha']

In [5]: %who_ls str
Out[5]: ['beta']
whos(parameter_s='')

Like %who, but gives some extra information about each variable.

The same type filtering of %who can be applied here.

For all variables, the type is printed. Additionally it prints:

  • For {},[],(): their length.

  • For numpy arrays, a summary with shape, number of elements, typecode and size in memory.

  • Everything else: a string representation, snipping their middle if too long.

Examples

Define two variables and list them with whos:

In [1]: alpha = 123

In [2]: beta = 'test'

In [3]: %whos
Variable   Type        Data/Info
--------------------------------
alpha      int         123
beta       str         test
xdel(parameter_s='')

Delete a variable, trying to clear it from anywhere that IPython’s machinery has references to it. By default, this uses the identity of the named object in the user namespace to remove references held under other names. The object is also removed from the output history.

Options

-n : Delete the specified name from all namespaces, without checking their identity.