When debugging, I often drop into an IPython shell for interactive code testing. However this also causes a large info dump of the python version, date and help instructions to stdout. How can I suppress this information so it doesn't obscure the intentional debugging messages?
x = get_important_data()
print 'important info! right here! The data is in variable "x"'
import IPython
IPython.embed()
This code gives output like this...
important info! right here! The data is in variable "x"
Python 2.7.11 |Anaconda 2.4.0 (x86_64)| (default, Dec 6 2015, 18:57:58)
Type "copyright", "credits" or "license" for more information.
IPython 4.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
You can do this:
IPython.embed(banner1="")
Setting banner1 to empty string would make the startup messages go away. It will not actually remove the messages but will replace them with empty strings.
You can also add useful messages using the banner1, banner2 and exit_msg parameters:
IPython.embed(
banner1="Entering Debug Mode",
banner2="Here's another helpful message",
exit_msg="Exiting the debug mode!"
)
If you ever need to launch IPython instance from the command line, you can do this:
ipython --no-banner
Related
I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
If I then run this program with
python my_example_program.py
on the first iteration through the loop I get put into an ipython shell and can inspect item and the environment as I would like to.
On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
There's a %kill_embedded command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
And here's the output:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD (06.03.2016): Seems that the %kill_embedded feature is kind of broken in IPython 4.0; you can use %exit_raise which will raise an exception and return back to the shell.
I'm trying to set a custom banner for the IPython qtconsole (v3.0.0). In my profile configuration, I set c.IPythonWidget.banner = u'Custom Banner', and then launch ipython qtconsole --profile=myprof. What I get is my custom banner pre-pended to the regular banner:
Custom BannerPython 2.7.5 (default, Mar 9 2014, 22:15:05)
Type "copyright", "credits" or "license" for more information.
IPython 3.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
%guiref -> A brief reference about the graphical user interface.
IPython profile: myprof
In [1]:
How can I get ONLY my banner to print, e.g.
Custom Banner
IPython profile: myprof
In [1]:
Thanks.
It's not ideal, but I was able to suppress the default banner by setting default_gui_banner=""in the core/usage.py module in the IPython source. I was not able to find any way to do this without modifying the source (which is ugly), so if anyone has a better way, I'm all ears.
The message you don't want is printed as part of the kernel's banner (which it gets from its shell). I was able to avoid printing it by setting the shell's banner1 attribute to an empty string before using/connecting to the kernel elsewhere:
from IPython.qt.inprocess import QtInProcessKernelManager
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel()
kernel_manager.kernel.shell.banner1 = ""
I've only worked with/tested this with the IPython Qt stuff, so this might not work for other kernels.
I have some code like this:
form IPython import embed
for item in my_item_list:
embed()
If I then run this program with
python my_example_program.py
on the first iteration through the loop I get put into an ipython shell and can inspect item and the environment as I would like to.
On quitting ipython the loop resumes and then I can inspect the next item and the environment as you would expect.
Is there a way for me to quit this code from within ipython (so that I am returned to a shell prompt). in any way short of opening another shell and killing the process?
There's a %kill_embedded command in IPython.
It doesn't put you directly back to the shell prompt, but it skips the other embed instances.
from IPython import embed
for item in range(5):
print 'embedding', item
embed()
And here's the output:
$ python my_example_program.py
embedding 0
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: print item
0
In [2]: ^D
embedding 1
Python 2.7.9 (default, Dec 13 2014, 22:30:33)
Type "copyright", "credits" or "license" for more information.
IPython 1.1.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [2]: %kill_embedded
Are you sure you want to kill this embedded instance (y/n)? [y/N] y
This embedded IPython will not reactivate anymore once you exit.
In [3]: print item
1
In [4]:
embedding 2
embedding 3
embedding 4
$
UPD (06.03.2016): Seems that the %kill_embedded feature is kind of broken in IPython 4.0; you can use %exit_raise which will raise an exception and return back to the shell.
So I've starting using IPython on my Mac. The !! operator, which is supposed to execute a shell command and get the output as useful data, is generating syntax errors. It appears to be just interpreting it as (! (!ls)), and spitting out !ls: command not found. I can't google exclamation marks and I didn't know where else to turn
I think you probably only want a single exclamation mark [docs], at least if you want to do anything with the output. For example:
localhost-2:tmp $ ipython
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
Type "copyright", "credits" or "license" for more information.
IPython 0.12 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: !ls
a.txt
In [2]: z = !ls
In [3]: z
Out[3]: ['a.txt']
In [4]: !!ls
Out[4]: ['a.txt']
but (which is what I'm assuming you're seeing)
In [10]: z = !!ls
In [11]: z
Out[11]: ['/bin/sh: !ls: command not found']
You can type %sx? for more information about what !!ls actually does.
The !! shortcut is an alias for the %sx ls magic command. This was introduced recently so your version may not have that functionality.
I install ipython, and then i run
./paster shell dev.ini
command, paster open standard python console. How can I make it run ipython?
Here's how it worked for me on Fedora 17 with IPython 0.12, paste-1.7.5.1 and pylons 1.0:
$ paster shell dev.ini
Pylons Interactive Shell
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)]
All objects from project.lib.base are available
Additional Objects:
mapper - Routes mapper object
wsgiapp - This project's WSGI App instance
app - paste.fixture wrapped around wsgiapp
>>> __name__ = '__main__'
>>> import IPython
>>> IPython.embed()
Python 2.7.3 (default, Jul 24 2012, 10:05:38)
Type "copyright", "credits" or "license" for more information.
IPython 0.12 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
Resetting __name__ is necessary because either Pylons/Paste sets __name__ to "pylons-admin" which confuses IPython (it tries to lookup the main module by its name in sys.modules).
I solved this problem downgrading ipython to version 0.10
Did you try the steps from the Pylons Quick Site Development:
13.1.2 Using an IPython embedded shell
IPython provides a more powerfull interactive prompt and a powerful
embedded shell. If you are a Python programmer and have not yet tried
IPython, you definitely should look into it.
First, import from IPython -- Add something like the following at the
top of your controller module, in our case in
firstapp/controllers/firstcontroller.py:
from IPython.Shell import IPShellEmbed args = ['-pdb', '-pi1', 'In
<\#>: ', '-pi2', ' .\D.: ',
'-po', 'Out<\#>: ', '-nosep'] ipshell = IPShellEmbed(args,
banner = 'Entering IPython. Press Ctrl-D to exit.',
exit_msg = 'Leaving Interpreter, back to Pylons.')
Then, place this code in your action/method:
ipshell('We are at action abc')
Return to Pylons and continue on responding to the request by pressing
Ctrl-D.
Note that because of some idiosyncratic feature of
IPython.Shell.IPShellEmbed, I had to put the following before each
call to ipshell():
ipshell.IP.exit_now = False ipshell('We are at action abc')
Is there any chance that your ipython is installed globally, but that you're running pylons from a virtualenv that is --no-site-packages? If that's the case, then pylons won't see your installation of ipython.
Ipython updated to version .11 just recently. Some of the components no longer work correctly, especially with 3rd party libraries. Check the mailing lists for pylons, and you may want to consider a bug report.
Try this:
paster ishell dev.ini