Show More
@@ -607,78 +607,65 b' startup files, and everything, just as if it were a normal IPython session.' | |||
|
607 | 607 | For information on setting configuration options when running IPython from |
|
608 | 608 | python, see :ref:`configure_start_ipython`. |
|
609 | 609 | |
|
610 |
It is also possible to embed an IPython shell in a namespace in your Python |
|
|
611 | This allows you to evaluate dynamically the state of your code, | |
|
612 |
|
|
|
613 |
|
|
|
610 | It is also possible to embed an IPython shell in a namespace in your Python | |
|
611 | code. This allows you to evaluate dynamically the state of your code, operate | |
|
612 | with your variables, analyze them, etc. For example, if you run the following | |
|
613 | code snippet:: | |
|
614 | 614 |
|
|
615 | ``` python | |
|
616 | import IPython | |
|
615 | import IPython | |
|
617 | 616 | |
|
618 | a = 42 | |
|
619 | IPython.embed() | |
|
620 | ``` | |
|
617 | a = 42 | |
|
618 | IPython.embed() | |
|
621 | 619 |
|
|
622 | 620 | and within the IPython shell, you reassign `a` to `23` to do further testing of |
|
623 |
some sort, you can then exit |
|
|
621 | some sort, you can then exit:: | |
|
624 | 622 |
|
|
625 | ``` | |
|
626 | >>> IPython.embed() | |
|
627 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
|
628 | Type 'copyright', 'credits' or 'license' for more information | |
|
629 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
|
623 | >>> IPython.embed() | |
|
624 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
|
625 | Type 'copyright', 'credits' or 'license' for more information | |
|
626 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
|
630 | 627 | |
|
631 | In [1]: a = 23 | |
|
628 | In [1]: a = 23 | |
|
632 | 629 | |
|
633 | In [2]: exit() | |
|
634 | ``` | |
|
630 | In [2]: exit() | |
|
635 | 631 |
|
|
636 | Once you exit and print `a`, the value 23 will be returned: | |
|
632 | Once you exit and print `a`, the value 23 will be returned:: | |
|
637 | 633 | |
|
638 | 634 | |
|
639 | ``` python | |
|
640 | print(a) | |
|
641 | ``` | |
|
642 | ``` | |
|
643 | 23 | |
|
644 | ``` | |
|
635 | In: print(a) | |
|
636 | 23 | |
|
645 | 637 | |
|
646 | 638 | It's important to note that the code run in the embedded IPython shell will |
|
647 | 639 | *not* change the state of your code and variables, **unless** the shell is |
|
648 | 640 | contained within the global namespace. In the above example, `a` is changed |
|
649 | 641 | because this is true. |
|
650 | 642 | |
|
651 | To further exemplify this, consider the following example: | |
|
652 |
|
|
|
653 | ``` python | |
|
654 | import IPython | |
|
655 | def do(): | |
|
656 | a = 42 | |
|
657 | print(a) | |
|
658 | IPython.embed() | |
|
659 | print(a) | |
|
660 | ``` | |
|
661 | ||
|
662 | Now if call the function and complete the state changes as we did above, the | |
|
663 | value `42` will be returned. Again, this is because it's not in the global namespace. | |
|
664 | ||
|
665 | ``` python | |
|
666 | do() | |
|
667 | ``` | |
|
668 | ``` | |
|
669 | 42 | |
|
670 | >>> IPython.embed() | |
|
671 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
|
672 | Type 'copyright', 'credits' or 'license' for more information | |
|
673 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
|
674 | ||
|
675 | In [1]: a = 23 | |
|
676 | ||
|
677 | In [2]: exit() | |
|
678 | ``` | |
|
679 | ``` | |
|
680 | 42 | |
|
681 | ``` | |
|
643 | To further exemplify this, consider the following example:: | |
|
644 | ||
|
645 | import IPython | |
|
646 | def do(): | |
|
647 | a = 42 | |
|
648 | print(a) | |
|
649 | IPython.embed() | |
|
650 | print(a) | |
|
651 | ||
|
652 | Now if call the function and complete the state changes as we did above, the | |
|
653 | value `42` will be returned. Again, this is because it's not in the global | |
|
654 | namespace:: | |
|
655 | ||
|
656 | do() | |
|
657 | ||
|
658 | Running a file with the above code can lead to the following session:: | |
|
659 | ||
|
660 | >>> IPython.embed() | |
|
661 | Python 3.6.2 (default, Jul 17 2017, 16:44:45) | |
|
662 | Type 'copyright', 'credits' or 'license' for more information | |
|
663 | IPython 6.2.0.dev -- An enhanced Interactive Python. Type '?' for help. | |
|
664 | ||
|
665 | In [1]: a = 23 | |
|
666 | ||
|
667 | In [2]: exit() | |
|
668 | 42 | |
|
682 | 669 |
|
|
683 | 670 | .. note:: |
|
684 | 671 |
General Comments 0
You need to be logged in to leave comments.
Login now