How to cancel interpreter command in emacs python-mode - python

If I mistype a command in the python intepreter, sometimes I just see .... For example if I type help(random.unif and press enter I cannot enter a new command. I have to exit emacs and start python and the interpreter again. Is there way to correct this?

As Pavel Anossov explains, you want to send Python a CTRL-C to interrupt it.
But in emacs, by default, CTRL-C is a prefix key.
Fortunately, in most interactive-shell modes, including python-mode and the alternatives, hitting CTRL-C twice in a row sends a ctrl-C to the interpreter. Or, more technically, CTRL-CCTRL-C is bound to comint-interrupt-subjob. (You can, of course, run it any other way—even META-X comint-interrupt-subjob if you really want.) The result looks like this:
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.
>>> help(f
... ^C ^C
KeyboardInterrupt
>>>
Another alternative is to use the quoted-insert command, usually bound to CTRL-Q, and then hit CTRL-C. However, because this will not interrupt the usual line input, you will usually have to follow it with a newline. The result looks like this:
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.
>>> help(f
... ^C
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>>

Usually CTRL-C works. Not sure about emacs-embedded interpreter. Alternatively, just provide the interpreter with whatever it's waiting for (in your example, an )).

Related

disable python greeting/version info

When I start a python interactive session from the command line I am greeted by :
Python 3.9.6 (default, Jun 30 2021, 10:22:16)
[GCC 11.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Is there a way of disabling that message so that I go immediately to the >>> prompt?
Yes, there is a way to do so.
Type in the cmd:
python -q
instead of
python
and this should do the trick.

Python shell under Mavericks: two sides of the readline.so bug

There is a problem with readline in Python shell under Mavericks. Two issues, answered here in stackoverflow separately but not both at the same time.
a) segfault after entering the 2nd line and/or
b) line editing does not work.
I provide links to both issues below.
Basically one can either A) do nothing and enjoy segfaults while typing a second line in the shell, or B) - "fix" it by renaming readline.so to readline.so.disabled -- but in this case loosing editing capabilities in the Python shell.
I could not find a recipe that solves both issues. And all of the sudden it's really hard to use Python shell. Please help.
Here are more details:
a) The segfault issue is answered here and here:
Python crashing when running two commands (Segmentation Fault: 11)
Segmentation fault: 11 in OS X
b) the "up arrow" issue in python interpreter is answered here:
Seeing escape characters when pressing the arrow keys in python shell
Here is how to reproduce the problem on my system:
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> y=2
Segmentation fault
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%sudo mv ./lib/python2.7/lib-dynload/readline.so ./lib/python2.7/lib-dynload/readline.so.disabled
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> y=2
>>> ^[[A
File "<stdin>", line 1
^
SyntaxError: invalid syntax
This will not fix your system install, but I think a virtualenv would not have this problem. I use Python every day on Mavericks with a virtualenv and never have any trouble.

Using Appkit and Python to hide mouse cursor on OSX

I'm trying to script hiding the mouse cursor on OSX 10.9. I have Chrome starting and going full screen for a kiosk and I'd like to periodically run a script to hide the cursor.
Applescript no longer directly supports "call method" to call the objective C method, so I thought the simplest method would be to use AppKit from the provided python.
It crashes:
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import AppKit
>>> AppKit.NSCursor.hide()
Assertion failed: (CGAtomicGet(&is_initialized)), function CGSConnectionByID, file Services/Connection/CGSConnection.c, line 123.
Abort trap: 6
I suspect there is a pre-requisite call I need to make to initialize something, but I haven't found anything yet while digging through docs/google.
What am I missing?
I've had luck with the Quartz included with PyObjC:
import Quartz
Quartz.CGDisplayHideCursor(Quartz.CGMainDisplayID())

How to surpress python's start-up information?

When I type "python" and return in shell, the following lines will come out:
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
How to surpress these lines please?
An easy way is to call Python as python -i -c "". This will also disable any start-up scripts, though. If you have a start-up script, you can also use python -i ~/.pythonrc.py (or however that script is named).

os.exec on Windows

I have a script that calls os.execvp into another Python instance. After doing this, I appear to be attached to a cmd.exe instance, not the Python instance I just created. The Python instance responds to Ctrl+C however.
H:\bin>Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hi')
Can't find file ('hi')
H:\bin>
H:\bin>
KeyboardInterrupt
>>> echo hi
hi
The call to exec:
from sys import argv
os.execvp('python', argv)
How do I replace the original Python instance with the new one, as per the behaviour one might see on Linux?
On Unix executing binaries is split into two stages - fork(3) to clone current process and exec(3) to load executable into address space. On windows there is only CreateProcess which does the same thing as fork+exec.
For portability your best bet is to use subprocess.Popen (which also does proper filename quoting on Windows unlike os.* counterparts) as in http://docs.python.org/library/subprocess.html#replacing-the-os-spawn-family

Categories

Resources