| 1 |
===================================== |
| 2 |
Introduction to IPython configuration |
| 3 |
===================================== |
| 4 |
|
| 5 |
.. _setting_config: |
| 6 |
|
| 7 |
Setting configurable options |
| 8 |
============================ |
| 9 |
|
| 10 |
Many of IPython's classes have configurable attributes (see |
| 11 |
:doc:`options/index` for the list). These can be |
| 12 |
configured in several ways. |
| 13 |
|
| 14 |
Python configuration files |
| 15 |
-------------------------- |
| 16 |
|
| 17 |
To create the blank configuration files, run:: |
| 18 |
|
| 19 |
ipython profile create [profilename] |
| 20 |
|
| 21 |
If you leave out the profile name, the files will be created for the |
| 22 |
``default`` profile (see :ref:`profiles`). These will typically be located in |
| 23 |
:file:`~/.ipython/profile_default/`, and will be named |
| 24 |
:file:`ipython_config.py`, for historical reasons you may also find files |
| 25 |
named with IPython prefix instead of Jupyter: |
| 26 |
:file:`ipython_notebook_config.py`, etc. The settings in |
| 27 |
:file:`ipython_config.py` apply to all IPython commands. |
| 28 |
|
| 29 |
By default, configuration files are fully featured Python scripts that can |
| 30 |
execute arbitrary code, the main usage is to set value on the configuration |
| 31 |
object ``c`` which exist in your configuration file. |
| 32 |
|
| 33 |
You can then configure class attributes like this:: |
| 34 |
|
| 35 |
c.InteractiveShell.automagic = False |
| 36 |
|
| 37 |
Be careful with spelling--incorrect names will simply be ignored, with |
| 38 |
no error. |
| 39 |
|
| 40 |
To add to a collection which may have already been defined elsewhere or have |
| 41 |
default values, you can use methods like those found on lists, dicts and |
| 42 |
sets: append, extend, :meth:`~traitlets.config.LazyConfigValue.prepend` (like |
| 43 |
extend, but at the front), add and update (which works both for dicts and |
| 44 |
sets):: |
| 45 |
|
| 46 |
c.InteractiveShellApp.extensions.append('Cython') |
| 47 |
|
| 48 |
.. versionadded:: 2.0 |
| 49 |
list, dict and set methods for config values |
| 50 |
|
| 51 |
Example configuration file |
| 52 |
`````````````````````````` |
| 53 |
|
| 54 |
:: |
| 55 |
|
| 56 |
# sample ipython_config.py |
| 57 |
|
| 58 |
c.TerminalIPythonApp.display_banner = True |
| 59 |
c.InteractiveShellApp.log_level = 20 |
| 60 |
c.InteractiveShellApp.extensions = [ |
| 61 |
'myextension' |
| 62 |
] |
| 63 |
c.InteractiveShellApp.exec_lines = [ |
| 64 |
'import numpy', |
| 65 |
'import scipy' |
| 66 |
] |
| 67 |
c.InteractiveShellApp.exec_files = [ |
| 68 |
'mycode.py', |
| 69 |
'fancy.ipy' |
| 70 |
] |
| 71 |
c.InteractiveShell.colors = 'lightbg' |
| 72 |
c.InteractiveShell.xmode = 'Context' |
| 73 |
c.TerminalInteractiveShell.confirm_exit = False |
| 74 |
c.TerminalInteractiveShell.editor = 'nano' |
| 75 |
|
| 76 |
c.PrefilterManager.multi_line_specials = True |
| 77 |
|
| 78 |
c.AliasManager.user_aliases = [ |
| 79 |
('la', 'ls -al') |
| 80 |
] |
| 81 |
|
| 82 |
JSON Configuration files |
| 83 |
------------------------ |
| 84 |
|
| 85 |
In case where executability of configuration can be problematic, or |
| 86 |
configurations need to be modified programmatically, IPython also support a |
| 87 |
limited set of functionalities via ``.json`` configuration files. |
| 88 |
|
| 89 |
You can define most of the configuration options via a JSON object whose |
| 90 |
hierarchy represents the value you would normally set on the ``c`` object of |
| 91 |
``.py`` configuration files. The following ``ipython_config.json`` file:: |
| 92 |
|
| 93 |
{ |
| 94 |
"InteractiveShell": { |
| 95 |
"colors": "lightbg", |
| 96 |
}, |
| 97 |
"InteractiveShellApp": { |
| 98 |
"extensions": [ |
| 99 |
"myextension" |
| 100 |
] |
| 101 |
}, |
| 102 |
"TerminalInteractiveShell": { |
| 103 |
"editor": "nano" |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
Is equivalent to the following ``ipython_config.py``:: |
| 108 |
|
| 109 |
c.InteractiveShell.colors = 'lightbg' |
| 110 |
c.InteractiveShellApp.extensions = [ |
| 111 |
'myextension' |
| 112 |
] |
| 113 |
c.TerminalInteractiveShell.editor = 'nano' |
| 114 |
|
| 115 |
|
| 116 |
Command line arguments |
| 117 |
---------------------- |
| 118 |
|
| 119 |
Every configurable value can be set from the command line, using this |
| 120 |
syntax:: |
| 121 |
|
| 122 |
ipython --ClassName.attribute=value |
| 123 |
|
| 124 |
Many frequently used options have short aliases and flags, such as |
| 125 |
``--matplotlib`` (to integrate with a matplotlib GUI event loop) or |
| 126 |
``--pdb`` (automatic post-mortem debugging of exceptions). |
| 127 |
|
| 128 |
To see all of these abbreviated options, run:: |
| 129 |
|
| 130 |
ipython --help |
| 131 |
jupyter notebook --help |
| 132 |
# etc. |
| 133 |
|
| 134 |
Options specified at the command line, in either format, override |
| 135 |
options set in a configuration file. |
| 136 |
|
| 137 |
The config magic |
| 138 |
---------------- |
| 139 |
|
| 140 |
You can also modify config from inside IPython, using a magic command:: |
| 141 |
|
| 142 |
%config IPCompleter.greedy = True |
| 143 |
|
| 144 |
At present, this only affects the current session - changes you make to |
| 145 |
config are not saved anywhere. Also, some options are only read when |
| 146 |
IPython starts, so they can't be changed like this. |
| 147 |
|
| 148 |
.. _configure_start_ipython: |
| 149 |
|
| 150 |
Running IPython from Python |
| 151 |
---------------------------- |
| 152 |
|
| 153 |
If you are using :ref:`embedding` to start IPython from a normal |
| 154 |
python file, you can set configuration options the same way as in a |
| 155 |
config file by creating a traitlets :class:`Config` object and passing it to |
| 156 |
start_ipython like in the example below. |
| 157 |
|
| 158 |
.. literalinclude:: ../../../examples/Embedding/start_ipython_config.py |
| 159 |
:language: python |
| 160 |
|
| 161 |
.. _profiles: |
| 162 |
|
| 163 |
Profiles |
| 164 |
======== |
| 165 |
|
| 166 |
IPython can use multiple profiles, with separate configuration and |
| 167 |
history. By default, if you don't specify a profile, IPython always runs |
| 168 |
in the ``default`` profile. To use a new profile:: |
| 169 |
|
| 170 |
ipython profile create foo # create the profile foo |
| 171 |
ipython --profile=foo # start IPython using the new profile |
| 172 |
|
| 173 |
Profiles are typically stored in :ref:`ipythondir`, but you can also keep |
| 174 |
a profile in the current working directory, for example to distribute it |
| 175 |
with a project. To find a profile directory on the filesystem:: |
| 176 |
|
| 177 |
ipython locate profile foo |
| 178 |
|
| 179 |
.. _ipythondir: |
| 180 |
|
| 181 |
The IPython directory |
| 182 |
===================== |
| 183 |
|
| 184 |
IPython stores its files---config, command history and extensions---in |
| 185 |
the directory :file:`~/.ipython/` by default. |
| 186 |
|
| 187 |
.. envvar:: IPYTHONDIR |
| 188 |
|
| 189 |
If set, this environment variable should be the path to a directory, |
| 190 |
which IPython will use for user data. IPython will create it if it |
| 191 |
does not exist. |
| 192 |
|
| 193 |
.. option:: --ipython-dir=<path> |
| 194 |
|
| 195 |
This command line option can also be used to override the default |
| 196 |
IPython directory. |
| 197 |
|
| 198 |
To see where IPython is looking for the IPython directory, use the command |
| 199 |
``ipython locate``, or the Python function :func:`IPython.paths.get_ipython_dir`. |
| 200 |
|
| 201 |
|
| 202 |
Systemwide configuration |
| 203 |
======================== |
| 204 |
|
| 205 |
It can be useful to deploy systemwide ipython or ipykernel configuration |
| 206 |
when managing environment for many users. At startup time IPython and |
| 207 |
IPykernel will search for configuration file in multiple systemwide |
| 208 |
locations, mainly: |
| 209 |
|
| 210 |
- ``/etc/ipython/`` |
| 211 |
- ``/usr/local/etc/ipython/`` |
| 212 |
|
| 213 |
When the global install is a standalone python distribution it may also |
| 214 |
search in distribution specific location, for example: |
| 215 |
|
| 216 |
- ``$ANACONDA_LOCATION/etc/ipython/`` |
| 217 |
|
| 218 |
In those locations, Terminal IPython will look for a file called |
| 219 |
``ipython_config.py`` and ``ipython_config.json``, ipykernel will look for |
| 220 |
``ipython_kernel_config.py`` and ``ipython_kernel.json``. |
| 221 |
|
| 222 |
Configuration files are loaded in order and merged with configuration on |
| 223 |
later location taking precedence on earlier locations (that is to say a user |
| 224 |
can overwrite a systemwide configuration option). |
| 225 |
|
| 226 |
You can see all locations in which IPython is looking for configuration files |
| 227 |
by starting ipython in debug mode:: |
| 228 |
|
| 229 |
$ ipython --debug -c 'exit()' |
| 230 |
|
| 231 |
Identically with ipykernel though the command is currently blocking until |
| 232 |
this process is killed with ``Ctrl-\``:: |
| 233 |
|
| 234 |
$ python -m ipykernel --debug |