Show More
@@ -1,11 +1,92 b'' | |||||
1 | """ |
|
1 | """ | |
2 | IPython extension: autoreload modules before executing the next line |
|
2 | ``autoreload`` is an IPython extension that reloads modules | |
|
3 | automatically before executing the line of code typed. | |||
3 |
|
4 | |||
4 | Try:: |
|
5 | This makes for example the following workflow possible: | |
5 |
|
6 | |||
6 | %autoreload? |
|
7 | .. sourcecode:: ipython | |
|
8 | ||||
|
9 | In [1]: %load_ext autoreload | |||
|
10 | ||||
|
11 | In [2]: %autoreload 2 | |||
|
12 | ||||
|
13 | In [3]: from foo import some_function | |||
|
14 | ||||
|
15 | In [4]: some_function() | |||
|
16 | Out[4]: 42 | |||
|
17 | ||||
|
18 | In [5]: # open foo.py in an editor and change some_function to return 43 | |||
|
19 | ||||
|
20 | In [6]: some_function() | |||
|
21 | Out[6]: 43 | |||
|
22 | ||||
|
23 | The module was reloaded without reloading it explicitly, and the | |||
|
24 | object imported with ``from foo import ...`` was also updated. | |||
|
25 | ||||
|
26 | Usage | |||
|
27 | ===== | |||
|
28 | ||||
|
29 | The following magic commands are provided: | |||
|
30 | ||||
|
31 | ``%autoreload`` | |||
|
32 | ||||
|
33 | Reload all modules (except those excluded by ``%aimport``) | |||
|
34 | automatically now. | |||
|
35 | ||||
|
36 | ``%autoreload 0`` | |||
|
37 | ||||
|
38 | Disable automatic reloading. | |||
|
39 | ||||
|
40 | ``%autoreload 1`` | |||
|
41 | ||||
|
42 | Reload all modules imported with ``%aimport`` every time before | |||
|
43 | executing the Python code typed. | |||
|
44 | ||||
|
45 | ``%autoreload 2`` | |||
|
46 | ||||
|
47 | Reload all modules (except those excluded by ``%aimport``) every | |||
|
48 | time before executing the Python code typed. | |||
|
49 | ||||
|
50 | ``%aimport`` | |||
|
51 | ||||
|
52 | List modules which are to be automatically imported or not to be imported. | |||
|
53 | ||||
|
54 | ``%aimport foo`` | |||
|
55 | ||||
|
56 | Import module 'foo' and mark it to be autoreloaded for ``%autoreload 1`` | |||
|
57 | ||||
|
58 | ``%aimport -foo`` | |||
|
59 | ||||
|
60 | Mark module 'foo' to not be autoreloaded. | |||
|
61 | ||||
|
62 | Caveats | |||
|
63 | ======= | |||
|
64 | ||||
|
65 | Reloading Python modules in a reliable way is in general difficult, | |||
|
66 | and unexpected things may occur. ``%autoreload`` tries to work around | |||
|
67 | common pitfalls by replacing function code objects and parts of | |||
|
68 | classes previously in the module with new versions. This makes the | |||
|
69 | following things to work: | |||
|
70 | ||||
|
71 | - Functions and classes imported via 'from xxx import foo' are upgraded | |||
|
72 | to new versions when 'xxx' is reloaded. | |||
|
73 | ||||
|
74 | - Methods and properties of classes are upgraded on reload, so that | |||
|
75 | calling 'c.foo()' on an object 'c' created before the reload causes | |||
|
76 | the new code for 'foo' to be executed. | |||
|
77 | ||||
|
78 | Some of the known remaining caveats are: | |||
|
79 | ||||
|
80 | - Replacing code objects does not always succeed: changing a @property | |||
|
81 | in a class to an ordinary method or a method to a member variable | |||
|
82 | can cause problems (but in old objects only). | |||
|
83 | ||||
|
84 | - Functions that are removed (eg. via monkey-patching) from a module | |||
|
85 | before it is reloaded are not upgraded. | |||
|
86 | ||||
|
87 | - C extension modules cannot be reloaded, and so cannot be | |||
|
88 | autoreloaded. | |||
7 |
|
89 | |||
8 | for documentation. |
|
|||
9 | """ |
|
90 | """ | |
10 |
|
91 | |||
11 | # Pauli Virtanen <pav@iki.fi>, 2008. |
|
92 | # Pauli Virtanen <pav@iki.fi>, 2008. |
General Comments 0
You need to be logged in to leave comments.
Login now