2

Based on this question I discovered how to fix the echoing problem in the python shell in emacs. What I want to do is add this to my .emacs file so that it will happen automatically.

(defun python-startup () 
  (setq comint-process-echoes t))

(add-hook 'py-shell-hook 'python-startup)

If I start a python shell (M-x python-shell), this hasn't worked.

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 22
22
22

I can run this function with M-: (python-startup), and then the echoing behavior stops.

>>> 22
22

I don't know if I'm setting up the hook incorrectly, or if I should be using a different hook altogether. As a side note, how do I know what hook is called for what function? The end goal is to end up being able to use :results output :session in org-mode so that I can integrate python code without the results echoing every command. I suspect that once I fix the hook, that is the behavior I will have, but I don't actually know if this is true.

1
  • In my version of Emacs (23.1.1) and Python 2.7.1, the shell echoes properly by default. When I set comint-process-echoes as you defined, the echoing behavior stops. I can't reproduce the duplicate echoing that you have above, so I suspect the bug exists elsewhere. Commented Mar 22, 2012 at 21:28

1 Answer 1

3

My brief investigation into this shows that python-mode (as found in my Emacs) has no py-shell-hook, so naturally it won't run anything you put in there.

When I looked at python-mode, there are no hooks that it runs, so you are a bit out of luck.

Your best bet it to just make your own command, for exmaple:

(defun alex-python-shell ()
  "Start a python shell my way."
  (interactive)
  (python-shell)
  (python-startup))

If you need to call python-shell interactively, use

(call-interactively 'python-shell)
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, nice workaround for just starting up python myself. I wonder though, is python-mode only for viewing .py files, and not for the shell? If so, I wouldn't expect it to have a py-shell-hook or anything like that. Nevertheless, do you know of some list of hooks somewhere?
Sorry, I meant python-shell doesn't have any hooks. Just do: M-x find-function RET python-shell RET and look for hooks being run in the code. Easy-Breezy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.