##// END OF EJS Templates
Fix header levels in technical description of config
Thomas Kluyver -
Show More
@@ -1,566 +1,566 b''
1 .. _config_overview:
1 .. _config_overview:
2
2
3 ============================================
3 ============================================
4 Overview of the IPython configuration system
4 Overview of the IPython configuration system
5 ============================================
5 ============================================
6
6
7 This section describes the IPython configuration system.
7 This section describes the IPython configuration system.
8
8
9 The main concepts
9 The main concepts
10 =================
10 =================
11
11
12 There are a number of abstractions that the IPython configuration system uses.
12 There are a number of abstractions that the IPython configuration system uses.
13 Each of these abstractions is represented by a Python class.
13 Each of these abstractions is represented by a Python class.
14
14
15 Configuration object: :class:`~IPython.config.loader.Config`
15 Configuration object: :class:`~IPython.config.loader.Config`
16 A configuration object is a simple dictionary-like class that holds
16 A configuration object is a simple dictionary-like class that holds
17 configuration attributes and sub-configuration objects. These classes
17 configuration attributes and sub-configuration objects. These classes
18 support dotted attribute style access (``cfg.Foo.bar``) in addition to the
18 support dotted attribute style access (``cfg.Foo.bar``) in addition to the
19 regular dictionary style access (``cfg['Foo']['bar']``).
19 regular dictionary style access (``cfg['Foo']['bar']``).
20 The Config object is a wrapper around a simple dictionary with some convenience methods,
20 The Config object is a wrapper around a simple dictionary with some convenience methods,
21 such as merging and automatic section creation.
21 such as merging and automatic section creation.
22
22
23 Application: :class:`~IPython.config.application.Application`
23 Application: :class:`~IPython.config.application.Application`
24 An application is a process that does a specific job. The most obvious
24 An application is a process that does a specific job. The most obvious
25 application is the :command:`ipython` command line program. Each
25 application is the :command:`ipython` command line program. Each
26 application reads *one or more* configuration files and a single set of
26 application reads *one or more* configuration files and a single set of
27 command line options
27 command line options
28 and then produces a master configuration object for the application. This
28 and then produces a master configuration object for the application. This
29 configuration object is then passed to the configurable objects that the
29 configuration object is then passed to the configurable objects that the
30 application creates. These configurable objects implement the actual logic
30 application creates. These configurable objects implement the actual logic
31 of the application and know how to configure themselves given the
31 of the application and know how to configure themselves given the
32 configuration object.
32 configuration object.
33
33
34 Applications always have a `log` attribute that is a configured Logger.
34 Applications always have a `log` attribute that is a configured Logger.
35 This allows centralized logging configuration per-application.
35 This allows centralized logging configuration per-application.
36
36
37 Configurable: :class:`~IPython.config.configurable.Configurable`
37 Configurable: :class:`~IPython.config.configurable.Configurable`
38 A configurable is a regular Python class that serves as a base class for
38 A configurable is a regular Python class that serves as a base class for
39 all main classes in an application. The
39 all main classes in an application. The
40 :class:`~IPython.config.configurable.Configurable` base class is
40 :class:`~IPython.config.configurable.Configurable` base class is
41 lightweight and only does one things.
41 lightweight and only does one things.
42
42
43 This :class:`~IPython.config.configurable.Configurable` is a subclass
43 This :class:`~IPython.config.configurable.Configurable` is a subclass
44 of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure
44 of :class:`~IPython.utils.traitlets.HasTraits` that knows how to configure
45 itself. Class level traits with the metadata ``config=True`` become
45 itself. Class level traits with the metadata ``config=True`` become
46 values that can be configured from the command line and configuration
46 values that can be configured from the command line and configuration
47 files.
47 files.
48
48
49 Developers create :class:`~IPython.config.configurable.Configurable`
49 Developers create :class:`~IPython.config.configurable.Configurable`
50 subclasses that implement all of the logic in the application. Each of
50 subclasses that implement all of the logic in the application. Each of
51 these subclasses has its own configuration information that controls how
51 these subclasses has its own configuration information that controls how
52 instances are created.
52 instances are created.
53
53
54 Singletons: :class:`~IPython.config.configurable.SingletonConfigurable`
54 Singletons: :class:`~IPython.config.configurable.SingletonConfigurable`
55 Any object for which there is a single canonical instance. These are
55 Any object for which there is a single canonical instance. These are
56 just like Configurables, except they have a class method
56 just like Configurables, except they have a class method
57 :meth:`~IPython.config.configurable.SingletonConfigurable.instance`,
57 :meth:`~IPython.config.configurable.SingletonConfigurable.instance`,
58 that returns the current active instance (or creates one if it
58 that returns the current active instance (or creates one if it
59 does not exist). Examples of singletons include
59 does not exist). Examples of singletons include
60 :class:`~IPython.config.application.Application`s and
60 :class:`~IPython.config.application.Application`s and
61 :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets
61 :class:`~IPython.core.interactiveshell.InteractiveShell`. This lets
62 objects easily connect to the current running Application without passing
62 objects easily connect to the current running Application without passing
63 objects around everywhere. For instance, to get the current running
63 objects around everywhere. For instance, to get the current running
64 Application instance, simply do: ``app = Application.instance()``.
64 Application instance, simply do: ``app = Application.instance()``.
65
65
66
66
67 .. note::
67 .. note::
68
68
69 Singletons are not strictly enforced - you can have many instances
69 Singletons are not strictly enforced - you can have many instances
70 of a given singleton class, but the :meth:`instance` method will always
70 of a given singleton class, but the :meth:`instance` method will always
71 return the same one.
71 return the same one.
72
72
73 Having described these main concepts, we can now state the main idea in our
73 Having described these main concepts, we can now state the main idea in our
74 configuration system: *"configuration" allows the default values of class
74 configuration system: *"configuration" allows the default values of class
75 attributes to be controlled on a class by class basis*. Thus all instances of
75 attributes to be controlled on a class by class basis*. Thus all instances of
76 a given class are configured in the same way. Furthermore, if two instances
76 a given class are configured in the same way. Furthermore, if two instances
77 need to be configured differently, they need to be instances of two different
77 need to be configured differently, they need to be instances of two different
78 classes. While this model may seem a bit restrictive, we have found that it
78 classes. While this model may seem a bit restrictive, we have found that it
79 expresses most things that need to be configured extremely well. However, it
79 expresses most things that need to be configured extremely well. However, it
80 is possible to create two instances of the same class that have different
80 is possible to create two instances of the same class that have different
81 trait values. This is done by overriding the configuration.
81 trait values. This is done by overriding the configuration.
82
82
83 Now, we show what our configuration objects and files look like.
83 Now, we show what our configuration objects and files look like.
84
84
85 Configuration objects and files
85 Configuration objects and files
86 ===============================
86 ===============================
87
87
88 A configuration object is little more than a wrapper around a dictionary.
88 A configuration object is little more than a wrapper around a dictionary.
89 A configuration *file* is simply a mechanism for producing that object.
89 A configuration *file* is simply a mechanism for producing that object.
90 The main IPython configuration file is a plain Python script,
90 The main IPython configuration file is a plain Python script,
91 which can perform extensive logic to populate the config object.
91 which can perform extensive logic to populate the config object.
92 IPython 2.0 introduces a JSON configuration file,
92 IPython 2.0 introduces a JSON configuration file,
93 which is just a direct JSON serialization of the config dictionary,
93 which is just a direct JSON serialization of the config dictionary,
94 which is easily processed by external software.
94 which is easily processed by external software.
95
95
96 When both Python and JSON configuration file are present, both will be loaded,
96 When both Python and JSON configuration file are present, both will be loaded,
97 with JSON configuration having higher priority.
97 with JSON configuration having higher priority.
98
98
99 Python configuration Files
99 Python configuration Files
100 ~~~~~~~~~~~~~~~~~~~~~~~~~~
100 --------------------------
101
101
102 A Python configuration file is a pure Python file that populates a configuration object.
102 A Python configuration file is a pure Python file that populates a configuration object.
103 This configuration object is a :class:`~IPython.config.loader.Config` instance.
103 This configuration object is a :class:`~IPython.config.loader.Config` instance.
104 While in a configuration file, to get a reference to this object, simply call the :func:`get_config`
104 While in a configuration file, to get a reference to this object, simply call the :func:`get_config`
105 function, which is available in the global namespace of the script.
105 function, which is available in the global namespace of the script.
106
106
107 Here is an example of a super simple configuration file that does nothing::
107 Here is an example of a super simple configuration file that does nothing::
108
108
109 c = get_config()
109 c = get_config()
110
110
111 Once you get a reference to the configuration object, you simply set
111 Once you get a reference to the configuration object, you simply set
112 attributes on it. All you have to know is:
112 attributes on it. All you have to know is:
113
113
114 * The name of the class to configure.
114 * The name of the class to configure.
115 * The name of the attribute.
115 * The name of the attribute.
116 * The type of each attribute.
116 * The type of each attribute.
117
117
118 The answers to these questions are provided by the various
118 The answers to these questions are provided by the various
119 :class:`~IPython.config.configurable.Configurable` subclasses that an
119 :class:`~IPython.config.configurable.Configurable` subclasses that an
120 application uses. Let's look at how this would work for a simple configurable
120 application uses. Let's look at how this would work for a simple configurable
121 subclass::
121 subclass::
122
122
123 # Sample configurable:
123 # Sample configurable:
124 from IPython.config.configurable import Configurable
124 from IPython.config.configurable import Configurable
125 from IPython.utils.traitlets import Int, Float, Unicode, Bool
125 from IPython.utils.traitlets import Int, Float, Unicode, Bool
126
126
127 class MyClass(Configurable):
127 class MyClass(Configurable):
128 name = Unicode(u'defaultname', config=True)
128 name = Unicode(u'defaultname', config=True)
129 ranking = Int(0, config=True)
129 ranking = Int(0, config=True)
130 value = Float(99.0)
130 value = Float(99.0)
131 # The rest of the class implementation would go here..
131 # The rest of the class implementation would go here..
132
132
133 In this example, we see that :class:`MyClass` has three attributes, two
133 In this example, we see that :class:`MyClass` has three attributes, two
134 of which (``name``, ``ranking``) can be configured. All of the attributes
134 of which (``name``, ``ranking``) can be configured. All of the attributes
135 are given types and default values. If a :class:`MyClass` is instantiated,
135 are given types and default values. If a :class:`MyClass` is instantiated,
136 but not configured, these default values will be used. But let's see how
136 but not configured, these default values will be used. But let's see how
137 to configure this class in a configuration file::
137 to configure this class in a configuration file::
138
138
139 # Sample config file
139 # Sample config file
140 c = get_config()
140 c = get_config()
141
141
142 c.MyClass.name = 'coolname'
142 c.MyClass.name = 'coolname'
143 c.MyClass.ranking = 10
143 c.MyClass.ranking = 10
144
144
145 After this configuration file is loaded, the values set in it will override
145 After this configuration file is loaded, the values set in it will override
146 the class defaults anytime a :class:`MyClass` is created. Furthermore,
146 the class defaults anytime a :class:`MyClass` is created. Furthermore,
147 these attributes will be type checked and validated anytime they are set.
147 these attributes will be type checked and validated anytime they are set.
148 This type checking is handled by the :mod:`IPython.utils.traitlets` module,
148 This type checking is handled by the :mod:`IPython.utils.traitlets` module,
149 which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types.
149 which provides the :class:`Unicode`, :class:`Int` and :class:`Float` types.
150 In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides
150 In addition to these traitlets, the :mod:`IPython.utils.traitlets` provides
151 traitlets for a number of other types.
151 traitlets for a number of other types.
152
152
153 .. note::
153 .. note::
154
154
155 Underneath the hood, the :class:`Configurable` base class is a subclass of
155 Underneath the hood, the :class:`Configurable` base class is a subclass of
156 :class:`IPython.utils.traitlets.HasTraits`. The
156 :class:`IPython.utils.traitlets.HasTraits`. The
157 :mod:`IPython.utils.traitlets` module is a lightweight version of
157 :mod:`IPython.utils.traitlets` module is a lightweight version of
158 :mod:`enthought.traits`. Our implementation is a pure Python subset
158 :mod:`enthought.traits`. Our implementation is a pure Python subset
159 (mostly API compatible) of :mod:`enthought.traits` that does not have any
159 (mostly API compatible) of :mod:`enthought.traits` that does not have any
160 of the automatic GUI generation capabilities. Our plan is to achieve 100%
160 of the automatic GUI generation capabilities. Our plan is to achieve 100%
161 API compatibility to enable the actual :mod:`enthought.traits` to
161 API compatibility to enable the actual :mod:`enthought.traits` to
162 eventually be used instead. Currently, we cannot use
162 eventually be used instead. Currently, we cannot use
163 :mod:`enthought.traits` as we are committed to the core of IPython being
163 :mod:`enthought.traits` as we are committed to the core of IPython being
164 pure Python.
164 pure Python.
165
165
166 It should be very clear at this point what the naming convention is for
166 It should be very clear at this point what the naming convention is for
167 configuration attributes::
167 configuration attributes::
168
168
169 c.ClassName.attribute_name = attribute_value
169 c.ClassName.attribute_name = attribute_value
170
170
171 Here, ``ClassName`` is the name of the class whose configuration attribute you
171 Here, ``ClassName`` is the name of the class whose configuration attribute you
172 want to set, ``attribute_name`` is the name of the attribute you want to set
172 want to set, ``attribute_name`` is the name of the attribute you want to set
173 and ``attribute_value`` the the value you want it to have. The ``ClassName``
173 and ``attribute_value`` the the value you want it to have. The ``ClassName``
174 attribute of ``c`` is not the actual class, but instead is another
174 attribute of ``c`` is not the actual class, but instead is another
175 :class:`~IPython.config.loader.Config` instance.
175 :class:`~IPython.config.loader.Config` instance.
176
176
177 .. note::
177 .. note::
178
178
179 The careful reader may wonder how the ``ClassName`` (``MyClass`` in
179 The careful reader may wonder how the ``ClassName`` (``MyClass`` in
180 the above example) attribute of the configuration object ``c`` gets
180 the above example) attribute of the configuration object ``c`` gets
181 created. These attributes are created on the fly by the
181 created. These attributes are created on the fly by the
182 :class:`~IPython.config.loader.Config` instance, using a simple naming
182 :class:`~IPython.config.loader.Config` instance, using a simple naming
183 convention. Any attribute of a :class:`~IPython.config.loader.Config`
183 convention. Any attribute of a :class:`~IPython.config.loader.Config`
184 instance whose name begins with an uppercase character is assumed to be a
184 instance whose name begins with an uppercase character is assumed to be a
185 sub-configuration and a new empty :class:`~IPython.config.loader.Config`
185 sub-configuration and a new empty :class:`~IPython.config.loader.Config`
186 instance is dynamically created for that attribute. This allows deeply
186 instance is dynamically created for that attribute. This allows deeply
187 hierarchical information created easily (``c.Foo.Bar.value``) on the fly.
187 hierarchical information created easily (``c.Foo.Bar.value``) on the fly.
188
188
189 JSON configuration Files
189 JSON configuration Files
190 ~~~~~~~~~~~~~~~~~~~~~~~~
190 ------------------------
191
191
192 A JSON configuration file is simply a file that contains a
192 A JSON configuration file is simply a file that contains a
193 :class:`~IPython.config.loader.Config` dictionary serialized to JSON.
193 :class:`~IPython.config.loader.Config` dictionary serialized to JSON.
194 A JSON configuration file has the same base name as a Python configuration file,
194 A JSON configuration file has the same base name as a Python configuration file,
195 but with a .json extension.
195 but with a .json extension.
196
196
197 Configuration described in previous section could be written as follows in a
197 Configuration described in previous section could be written as follows in a
198 JSON configuration file:
198 JSON configuration file:
199
199
200 .. sourcecode:: json
200 .. sourcecode:: json
201
201
202 {
202 {
203 "version": "1.0",
203 "version": "1.0",
204 "MyClass": {
204 "MyClass": {
205 "name": "coolname",
205 "name": "coolname",
206 "ranking": 10
206 "ranking": 10
207 }
207 }
208 }
208 }
209
209
210 JSON configuration files can be more easily generated or processed by programs
210 JSON configuration files can be more easily generated or processed by programs
211 or other languages.
211 or other languages.
212
212
213
213
214 Configuration files inheritance
214 Configuration files inheritance
215 ===============================
215 ===============================
216
216
217 .. note::
217 .. note::
218
218
219 This section only apply to Python configuration files.
219 This section only apply to Python configuration files.
220
220
221 Let's say you want to have different configuration files for various purposes.
221 Let's say you want to have different configuration files for various purposes.
222 Our configuration system makes it easy for one configuration file to inherit
222 Our configuration system makes it easy for one configuration file to inherit
223 the information in another configuration file. The :func:`load_subconfig`
223 the information in another configuration file. The :func:`load_subconfig`
224 command can be used in a configuration file for this purpose. Here is a simple
224 command can be used in a configuration file for this purpose. Here is a simple
225 example that loads all of the values from the file :file:`base_config.py`::
225 example that loads all of the values from the file :file:`base_config.py`::
226
226
227 # base_config.py
227 # base_config.py
228 c = get_config()
228 c = get_config()
229 c.MyClass.name = 'coolname'
229 c.MyClass.name = 'coolname'
230 c.MyClass.ranking = 100
230 c.MyClass.ranking = 100
231
231
232 into the configuration file :file:`main_config.py`::
232 into the configuration file :file:`main_config.py`::
233
233
234 # main_config.py
234 # main_config.py
235 c = get_config()
235 c = get_config()
236
236
237 # Load everything from base_config.py
237 # Load everything from base_config.py
238 load_subconfig('base_config.py')
238 load_subconfig('base_config.py')
239
239
240 # Now override one of the values
240 # Now override one of the values
241 c.MyClass.name = 'bettername'
241 c.MyClass.name = 'bettername'
242
242
243 In a situation like this the :func:`load_subconfig` makes sure that the
243 In a situation like this the :func:`load_subconfig` makes sure that the
244 search path for sub-configuration files is inherited from that of the parent.
244 search path for sub-configuration files is inherited from that of the parent.
245 Thus, you can typically put the two in the same directory and everything will
245 Thus, you can typically put the two in the same directory and everything will
246 just work.
246 just work.
247
247
248 You can also load configuration files by profile, for instance:
248 You can also load configuration files by profile, for instance:
249
249
250 .. sourcecode:: python
250 .. sourcecode:: python
251
251
252 load_subconfig('ipython_config.py', profile='default')
252 load_subconfig('ipython_config.py', profile='default')
253
253
254 to inherit your default configuration as a starting point.
254 to inherit your default configuration as a starting point.
255
255
256
256
257 Class based configuration inheritance
257 Class based configuration inheritance
258 =====================================
258 =====================================
259
259
260 There is another aspect of configuration where inheritance comes into play.
260 There is another aspect of configuration where inheritance comes into play.
261 Sometimes, your classes will have an inheritance hierarchy that you want
261 Sometimes, your classes will have an inheritance hierarchy that you want
262 to be reflected in the configuration system. Here is a simple example::
262 to be reflected in the configuration system. Here is a simple example::
263
263
264 from IPython.config.configurable import Configurable
264 from IPython.config.configurable import Configurable
265 from IPython.utils.traitlets import Int, Float, Unicode, Bool
265 from IPython.utils.traitlets import Int, Float, Unicode, Bool
266
266
267 class Foo(Configurable):
267 class Foo(Configurable):
268 name = Unicode(u'fooname', config=True)
268 name = Unicode(u'fooname', config=True)
269 value = Float(100.0, config=True)
269 value = Float(100.0, config=True)
270
270
271 class Bar(Foo):
271 class Bar(Foo):
272 name = Unicode(u'barname', config=True)
272 name = Unicode(u'barname', config=True)
273 othervalue = Int(0, config=True)
273 othervalue = Int(0, config=True)
274
274
275 Now, we can create a configuration file to configure instances of :class:`Foo`
275 Now, we can create a configuration file to configure instances of :class:`Foo`
276 and :class:`Bar`::
276 and :class:`Bar`::
277
277
278 # config file
278 # config file
279 c = get_config()
279 c = get_config()
280
280
281 c.Foo.name = u'bestname'
281 c.Foo.name = u'bestname'
282 c.Bar.othervalue = 10
282 c.Bar.othervalue = 10
283
283
284 This class hierarchy and configuration file accomplishes the following:
284 This class hierarchy and configuration file accomplishes the following:
285
285
286 * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be
286 * The default value for :attr:`Foo.name` and :attr:`Bar.name` will be
287 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also
287 'bestname'. Because :class:`Bar` is a :class:`Foo` subclass it also
288 picks up the configuration information for :class:`Foo`.
288 picks up the configuration information for :class:`Foo`.
289 * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be
289 * The default value for :attr:`Foo.value` and :attr:`Bar.value` will be
290 ``100.0``, which is the value specified as the class default.
290 ``100.0``, which is the value specified as the class default.
291 * The default value for :attr:`Bar.othervalue` will be 10 as set in the
291 * The default value for :attr:`Bar.othervalue` will be 10 as set in the
292 configuration file. Because :class:`Foo` is the parent of :class:`Bar`
292 configuration file. Because :class:`Foo` is the parent of :class:`Bar`
293 it doesn't know anything about the :attr:`othervalue` attribute.
293 it doesn't know anything about the :attr:`othervalue` attribute.
294
294
295
295
296 .. _ipython_dir:
296 .. _ipython_dir:
297
297
298 Configuration file location
298 Configuration file location
299 ===========================
299 ===========================
300
300
301 So where should you put your configuration files? IPython uses "profiles" for
301 So where should you put your configuration files? IPython uses "profiles" for
302 configuration, and by default, all profiles will be stored in the so called
302 configuration, and by default, all profiles will be stored in the so called
303 "IPython directory". The location of this directory is determined by the
303 "IPython directory". The location of this directory is determined by the
304 following algorithm:
304 following algorithm:
305
305
306 * If the ``ipython-dir`` command line flag is given, its value is used.
306 * If the ``ipython-dir`` command line flag is given, its value is used.
307
307
308 * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir`
308 * If not, the value returned by :func:`IPython.utils.path.get_ipython_dir`
309 is used. This function will first look at the :envvar:`IPYTHONDIR`
309 is used. This function will first look at the :envvar:`IPYTHONDIR`
310 environment variable and then default to :file:`~/.ipython`.
310 environment variable and then default to :file:`~/.ipython`.
311 Historical support for the :envvar:`IPYTHON_DIR` environment variable will
311 Historical support for the :envvar:`IPYTHON_DIR` environment variable will
312 be removed in a future release.
312 be removed in a future release.
313
313
314 For most users, the configuration directory will be :file:`~/.ipython`.
314 For most users, the configuration directory will be :file:`~/.ipython`.
315
315
316 Previous versions of IPython on Linux would use the XDG config directory,
316 Previous versions of IPython on Linux would use the XDG config directory,
317 creating :file:`~/.config/ipython` by default. We have decided to go
317 creating :file:`~/.config/ipython` by default. We have decided to go
318 back to :file:`~/.ipython` for consistency among systems. IPython will
318 back to :file:`~/.ipython` for consistency among systems. IPython will
319 issue a warning if it finds the XDG location, and will move it to the new
319 issue a warning if it finds the XDG location, and will move it to the new
320 location if there isn't already a directory there.
320 location if there isn't already a directory there.
321
321
322 Once the location of the IPython directory has been determined, you need to know
322 Once the location of the IPython directory has been determined, you need to know
323 which profile you are using. For users with a single configuration, this will
323 which profile you are using. For users with a single configuration, this will
324 simply be 'default', and will be located in
324 simply be 'default', and will be located in
325 :file:`<IPYTHONDIR>/profile_default`.
325 :file:`<IPYTHONDIR>/profile_default`.
326
326
327 The next thing you need to know is what to call your configuration file. The
327 The next thing you need to know is what to call your configuration file. The
328 basic idea is that each application has its own default configuration filename.
328 basic idea is that each application has its own default configuration filename.
329 The default named used by the :command:`ipython` command line program is
329 The default named used by the :command:`ipython` command line program is
330 :file:`ipython_config.py`, and *all* IPython applications will use this file.
330 :file:`ipython_config.py`, and *all* IPython applications will use this file.
331 Other applications, such as the parallel :command:`ipcluster` scripts or the
331 Other applications, such as the parallel :command:`ipcluster` scripts or the
332 QtConsole will load their own config files *after* :file:`ipython_config.py`. To
332 QtConsole will load their own config files *after* :file:`ipython_config.py`. To
333 load a particular configuration file instead of the default, the name can be
333 load a particular configuration file instead of the default, the name can be
334 overridden by the ``config_file`` command line flag.
334 overridden by the ``config_file`` command line flag.
335
335
336 To generate the default configuration files, do::
336 To generate the default configuration files, do::
337
337
338 $ ipython profile create
338 $ ipython profile create
339
339
340 and you will have a default :file:`ipython_config.py` in your IPython directory
340 and you will have a default :file:`ipython_config.py` in your IPython directory
341 under :file:`profile_default`. If you want the default config files for the
341 under :file:`profile_default`. If you want the default config files for the
342 :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the
342 :mod:`IPython.parallel` applications, add ``--parallel`` to the end of the
343 command-line args.
343 command-line args.
344
344
345
345
346 Locating these files
346 Locating these files
347 --------------------
347 --------------------
348
348
349 From the command-line, you can quickly locate the IPYTHONDIR or a specific
349 From the command-line, you can quickly locate the IPYTHONDIR or a specific
350 profile with:
350 profile with:
351
351
352 .. sourcecode:: bash
352 .. sourcecode:: bash
353
353
354 $ ipython locate
354 $ ipython locate
355 /home/you/.ipython
355 /home/you/.ipython
356
356
357 $ ipython locate profile foo
357 $ ipython locate profile foo
358 /home/you/.ipython/profile_foo
358 /home/you/.ipython/profile_foo
359
359
360 These map to the utility functions: :func:`IPython.utils.path.get_ipython_dir`
360 These map to the utility functions: :func:`IPython.utils.path.get_ipython_dir`
361 and :func:`IPython.utils.path.locate_profile` respectively.
361 and :func:`IPython.utils.path.locate_profile` respectively.
362
362
363
363
364 .. _profiles_dev:
364 .. _profiles_dev:
365
365
366 Profiles
366 Profiles
367 ========
367 ========
368
368
369 A profile is a directory containing configuration and runtime files, such as
369 A profile is a directory containing configuration and runtime files, such as
370 logs, connection info for the parallel apps, and your IPython command history.
370 logs, connection info for the parallel apps, and your IPython command history.
371
371
372 The idea is that users often want to maintain a set of configuration files for
372 The idea is that users often want to maintain a set of configuration files for
373 different purposes: one for doing numerical computing with NumPy and SciPy and
373 different purposes: one for doing numerical computing with NumPy and SciPy and
374 another for doing symbolic computing with SymPy. Profiles make it easy to keep a
374 another for doing symbolic computing with SymPy. Profiles make it easy to keep a
375 separate configuration files, logs, and histories for each of these purposes.
375 separate configuration files, logs, and histories for each of these purposes.
376
376
377 Let's start by showing how a profile is used:
377 Let's start by showing how a profile is used:
378
378
379 .. code-block:: bash
379 .. code-block:: bash
380
380
381 $ ipython --profile=sympy
381 $ ipython --profile=sympy
382
382
383 This tells the :command:`ipython` command line program to get its configuration
383 This tells the :command:`ipython` command line program to get its configuration
384 from the "sympy" profile. The file names for various profiles do not change. The
384 from the "sympy" profile. The file names for various profiles do not change. The
385 only difference is that profiles are named in a special way. In the case above,
385 only difference is that profiles are named in a special way. In the case above,
386 the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHONDIR>/profile_sympy`.
386 the "sympy" profile means looking for :file:`ipython_config.py` in :file:`<IPYTHONDIR>/profile_sympy`.
387
387
388 The general pattern is this: simply create a new profile with:
388 The general pattern is this: simply create a new profile with:
389
389
390 .. code-block:: bash
390 .. code-block:: bash
391
391
392 $ ipython profile create <name>
392 $ ipython profile create <name>
393
393
394 which adds a directory called ``profile_<name>`` to your IPython directory. Then
394 which adds a directory called ``profile_<name>`` to your IPython directory. Then
395 you can load this profile by adding ``--profile=<name>`` to your command line
395 you can load this profile by adding ``--profile=<name>`` to your command line
396 options. Profiles are supported by all IPython applications.
396 options. Profiles are supported by all IPython applications.
397
397
398 IPython ships with some sample profiles in :file:`IPython/config/profile`. If
398 IPython ships with some sample profiles in :file:`IPython/config/profile`. If
399 you create profiles with the name of one of our shipped profiles, these config
399 you create profiles with the name of one of our shipped profiles, these config
400 files will be copied over instead of starting with the automatically generated
400 files will be copied over instead of starting with the automatically generated
401 config files.
401 config files.
402
402
403 Security Files
403 Security Files
404 --------------
404 --------------
405
405
406 If you are using the notebook, qtconsole, or parallel code, IPython stores
406 If you are using the notebook, qtconsole, or parallel code, IPython stores
407 connection information in small JSON files in the active profile's security
407 connection information in small JSON files in the active profile's security
408 directory. This directory is made private, so only you can see the files inside. If
408 directory. This directory is made private, so only you can see the files inside. If
409 you need to move connection files around to other computers, this is where they will
409 you need to move connection files around to other computers, this is where they will
410 be. If you want your code to be able to open security files by name, we have a
410 be. If you want your code to be able to open security files by name, we have a
411 convenience function :func:`IPython.utils.path.get_security_file`, which will return
411 convenience function :func:`IPython.utils.path.get_security_file`, which will return
412 the absolute path to a security file from its filename and [optionally] profile
412 the absolute path to a security file from its filename and [optionally] profile
413 name.
413 name.
414
414
415 .. _startup_files:
415 .. _startup_files:
416
416
417 Startup Files
417 Startup Files
418 -------------
418 -------------
419
419
420 If you want some code to be run at the beginning of every IPython session with
420 If you want some code to be run at the beginning of every IPython session with
421 a particular profile, the easiest way is to add Python (``.py``) or
421 a particular profile, the easiest way is to add Python (``.py``) or
422 IPython (``.ipy``) scripts to your :file:`<profile>/startup` directory. Files
422 IPython (``.ipy``) scripts to your :file:`<profile>/startup` directory. Files
423 in this directory will always be executed as soon as the IPython shell is
423 in this directory will always be executed as soon as the IPython shell is
424 constructed, and before any other code or scripts you have specified. If you
424 constructed, and before any other code or scripts you have specified. If you
425 have multiple files in the startup directory, they will be run in
425 have multiple files in the startup directory, they will be run in
426 lexicographical order, so you can control the ordering by adding a '00-'
426 lexicographical order, so you can control the ordering by adding a '00-'
427 prefix.
427 prefix.
428
428
429
429
430 .. _commandline:
430 .. _commandline:
431
431
432 Command-line arguments
432 Command-line arguments
433 ======================
433 ======================
434
434
435 IPython exposes *all* configurable options on the command-line. The command-line
435 IPython exposes *all* configurable options on the command-line. The command-line
436 arguments are generated from the Configurable traits of the classes associated
436 arguments are generated from the Configurable traits of the classes associated
437 with a given Application. Configuring IPython from the command-line may look
437 with a given Application. Configuring IPython from the command-line may look
438 very similar to an IPython config file
438 very similar to an IPython config file
439
439
440 IPython applications use a parser called
440 IPython applications use a parser called
441 :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config
441 :class:`~IPython.config.loader.KeyValueLoader` to load values into a Config
442 object. Values are assigned in much the same way as in a config file:
442 object. Values are assigned in much the same way as in a config file:
443
443
444 .. code-block:: bash
444 .. code-block:: bash
445
445
446 $ ipython --InteractiveShell.use_readline=False --BaseIPythonApplication.profile='myprofile'
446 $ ipython --InteractiveShell.use_readline=False --BaseIPythonApplication.profile='myprofile'
447
447
448 Is the same as adding:
448 Is the same as adding:
449
449
450 .. sourcecode:: python
450 .. sourcecode:: python
451
451
452 c.InteractiveShell.use_readline=False
452 c.InteractiveShell.use_readline=False
453 c.BaseIPythonApplication.profile='myprofile'
453 c.BaseIPythonApplication.profile='myprofile'
454
454
455 to your config file. Key/Value arguments *always* take a value, separated by '='
455 to your config file. Key/Value arguments *always* take a value, separated by '='
456 and no spaces.
456 and no spaces.
457
457
458 Common Arguments
458 Common Arguments
459 ----------------
459 ----------------
460
460
461 Since the strictness and verbosity of the KVLoader above are not ideal for everyday
461 Since the strictness and verbosity of the KVLoader above are not ideal for everyday
462 use, common arguments can be specified as flags_ or aliases_.
462 use, common arguments can be specified as flags_ or aliases_.
463
463
464 Flags and Aliases are handled by :mod:`argparse` instead, allowing for more flexible
464 Flags and Aliases are handled by :mod:`argparse` instead, allowing for more flexible
465 parsing. In general, flags and aliases are prefixed by ``--``, except for those
465 parsing. In general, flags and aliases are prefixed by ``--``, except for those
466 that are single characters, in which case they can be specified with a single ``-``, e.g.:
466 that are single characters, in which case they can be specified with a single ``-``, e.g.:
467
467
468 .. code-block:: bash
468 .. code-block:: bash
469
469
470 $ ipython -i -c "import numpy; x=numpy.linspace(0,1)" --profile testing --colors=lightbg
470 $ ipython -i -c "import numpy; x=numpy.linspace(0,1)" --profile testing --colors=lightbg
471
471
472 Aliases
472 Aliases
473 *******
473 *******
474
474
475 For convenience, applications have a mapping of commonly used traits, so you don't have
475 For convenience, applications have a mapping of commonly used traits, so you don't have
476 to specify the whole class name:
476 to specify the whole class name:
477
477
478 .. code-block:: bash
478 .. code-block:: bash
479
479
480 $ ipython --profile myprofile
480 $ ipython --profile myprofile
481 # and
481 # and
482 $ ipython --profile='myprofile'
482 $ ipython --profile='myprofile'
483 # are equivalent to
483 # are equivalent to
484 $ ipython --BaseIPythonApplication.profile='myprofile'
484 $ ipython --BaseIPythonApplication.profile='myprofile'
485
485
486 Flags
486 Flags
487 *****
487 *****
488
488
489 Applications can also be passed **flags**. Flags are options that take no
489 Applications can also be passed **flags**. Flags are options that take no
490 arguments. They are simply wrappers for
490 arguments. They are simply wrappers for
491 setting one or more configurables with predefined values, often True/False.
491 setting one or more configurables with predefined values, often True/False.
492
492
493 For instance:
493 For instance:
494
494
495 .. code-block:: bash
495 .. code-block:: bash
496
496
497 $ ipcontroller --debug
497 $ ipcontroller --debug
498 # is equivalent to
498 # is equivalent to
499 $ ipcontroller --Application.log_level=DEBUG
499 $ ipcontroller --Application.log_level=DEBUG
500 # and
500 # and
501 $ ipython --matplotlib
501 $ ipython --matplotlib
502 # is equivalent to
502 # is equivalent to
503 $ ipython --matplotlib auto
503 $ ipython --matplotlib auto
504 # or
504 # or
505 $ ipython --no-banner
505 $ ipython --no-banner
506 # is equivalent to
506 # is equivalent to
507 $ ipython --TerminalIPythonApp.display_banner=False
507 $ ipython --TerminalIPythonApp.display_banner=False
508
508
509 Subcommands
509 Subcommands
510 -----------
510 -----------
511
511
512
512
513 Some IPython applications have **subcommands**. Subcommands are modeled after
513 Some IPython applications have **subcommands**. Subcommands are modeled after
514 :command:`git`, and are called with the form :command:`command subcommand
514 :command:`git`, and are called with the form :command:`command subcommand
515 [...args]`. Currently, the QtConsole is a subcommand of terminal IPython:
515 [...args]`. Currently, the QtConsole is a subcommand of terminal IPython:
516
516
517 .. code-block:: bash
517 .. code-block:: bash
518
518
519 $ ipython qtconsole --profile myprofile
519 $ ipython qtconsole --profile myprofile
520
520
521 and :command:`ipcluster` is simply a wrapper for its various subcommands (start,
521 and :command:`ipcluster` is simply a wrapper for its various subcommands (start,
522 stop, engines).
522 stop, engines).
523
523
524 .. code-block:: bash
524 .. code-block:: bash
525
525
526 $ ipcluster start --profile=myprofile -n 4
526 $ ipcluster start --profile=myprofile -n 4
527
527
528
528
529 To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``.
529 To see a list of the available aliases, flags, and subcommands for an IPython application, simply pass ``-h`` or ``--help``. And to see the full list of configurable options (*very* long), pass ``--help-all``.
530
530
531
531
532 Design requirements
532 Design requirements
533 ===================
533 ===================
534
534
535 Here are the main requirements we wanted our configuration system to have:
535 Here are the main requirements we wanted our configuration system to have:
536
536
537 * Support for hierarchical configuration information.
537 * Support for hierarchical configuration information.
538
538
539 * Full integration with command line option parsers. Often, you want to read
539 * Full integration with command line option parsers. Often, you want to read
540 a configuration file, but then override some of the values with command line
540 a configuration file, but then override some of the values with command line
541 options. Our configuration system automates this process and allows each
541 options. Our configuration system automates this process and allows each
542 command line option to be linked to a particular attribute in the
542 command line option to be linked to a particular attribute in the
543 configuration hierarchy that it will override.
543 configuration hierarchy that it will override.
544
544
545 * Configuration files that are themselves valid Python code. This accomplishes
545 * Configuration files that are themselves valid Python code. This accomplishes
546 many things. First, it becomes possible to put logic in your configuration
546 many things. First, it becomes possible to put logic in your configuration
547 files that sets attributes based on your operating system, network setup,
547 files that sets attributes based on your operating system, network setup,
548 Python version, etc. Second, Python has a super simple syntax for accessing
548 Python version, etc. Second, Python has a super simple syntax for accessing
549 hierarchical data structures, namely regular attribute access
549 hierarchical data structures, namely regular attribute access
550 (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to
550 (``Foo.Bar.Bam.name``). Third, using Python makes it easy for users to
551 import configuration attributes from one configuration file to another.
551 import configuration attributes from one configuration file to another.
552 Fourth, even though Python is dynamically typed, it does have types that can
552 Fourth, even though Python is dynamically typed, it does have types that can
553 be checked at runtime. Thus, a ``1`` in a config file is the integer '1',
553 be checked at runtime. Thus, a ``1`` in a config file is the integer '1',
554 while a ``'1'`` is a string.
554 while a ``'1'`` is a string.
555
555
556 * A fully automated method for getting the configuration information to the
556 * A fully automated method for getting the configuration information to the
557 classes that need it at runtime. Writing code that walks a configuration
557 classes that need it at runtime. Writing code that walks a configuration
558 hierarchy to extract a particular attribute is painful. When you have
558 hierarchy to extract a particular attribute is painful. When you have
559 complex configuration information with hundreds of attributes, this makes
559 complex configuration information with hundreds of attributes, this makes
560 you want to cry.
560 you want to cry.
561
561
562 * Type checking and validation that doesn't require the entire configuration
562 * Type checking and validation that doesn't require the entire configuration
563 hierarchy to be specified statically before runtime. Python is a very
563 hierarchy to be specified statically before runtime. Python is a very
564 dynamic language and you don't always know everything that needs to be
564 dynamic language and you don't always know everything that needs to be
565 configured when a program starts.
565 configured when a program starts.
566
566
General Comments 0
You need to be logged in to leave comments. Login now