IPython !! not working - python

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.

Related

How to interrupt the loop with IPython.embed() inside [duplicate]

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.

How do I suppress the IPython startup message?

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

IPython qtconsole custom banner

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.

Can I make ipython exit from the calling code?

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.

Why does the python interpreter not print input code and output in emacs python shell buffer for python-mode?

My current python setup in emacs is:
(setq py-shell-name "ipython") ;; sudo pip install ipython
(setq py-shell-name "/usr/local/bin/ipython")
(setq py-force-py-shell-name-p t)
(add-to-list 'load-path "~/.emacs.d/python-mode.el-6.1.0/")
(setq py-install-directory "~/.emacs.d/python-mode.el-6.1.0/")
(require 'python-mode)
(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))
(add-to-list 'interpreter-mode-alist '("python" . python-mode))
In a python file, I start the python interpreter with "C-c !" (py-shell). I evaluate region of codes with "C-c |" (py-execute-region).
If I execute each of the following line individually,
x = "hello world"
x
print(x)
I get the following in the python shell buffer:
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- 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]:
In [5]:
In [6]: hello world
In [7]:
If I use the default python interpreter, I get
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> >>> >>> hello world
>>>
Questions:
Are these behaviour correct (not showing which code is being evaluated, and not showing what a python interpreter would normally print)?
How do I set it so that the python shell prints output like a normal python interpreter in the terminal? (with input code and output printed)
I guess I'm used to ESS for R. Thanks.
Your are looking for feature, which might be implemented.
In cases like this, please file this as question at
https://answers.launchpad.net/python-mode
When done, use at bottom "(+) Create bug report".
It creates a ticket, which helps us to track the issue.
Alternatively you may create a Blueprint and link a ticket afterwards.
Done so far:
https://blueprints.launchpad.net/python-mode/+spec/print-input-code
Beside, your init displays some useless code - however not harmful. Second setting of "py-shell-name" takes the symbol of the first one.
"...-mode-alist" stuff is already done from python-mode, you must not care for.

Categories