Running interactive python script from emacs - python

I am a fairly proficient vim user, but friends of mine told me so much good stuff about emacs that I decided to give it a try -- especially after finding about the aptly-named evil mode...
Anyways, I am currently working on a python script that requires user input (a subclass of cmd.Cmd). In vim, if I wanted to try it, I could simply do :!python % and then could interact with my script, until it quits. In emacs, I tried M-! python script.py, which would indeed run the script in a separate buffer, but then RETURNs seems not to be sent back to the script, but are caught by the emacs buffer instead. I also tried to have a look at python-mode's C-c C-c, but this runs the script in some temporary directory, whereas I just want to run it in (pwd).
So, is there any canonical way of doing that?

I don't know about canonical, but if I needed to interact with a script I'd do M-xshellRET and run the script from there.
There's also M-xterminal-emulator for more serious terminal emulation, not just shell stuff.

I like to use the Emacs "compile" command to test/run my python scripts. M-XcompileRET will pull up the default "make -k" but if you delete that and put in the command line for your script (including options), subsequent "compiles" will provide the new "compile" command automatically. All the output from your script will appear in the compile buffer. (As opposed to the shell, this provides a nice clean buffer each time it is invoked. Good for searching and such. If you forget to save your script before your run, compile will ask you if you would like to save the file.)
You will lose your the command line when you restart Emacs. But you can get Emacs to set the compile-command for the buffer holding your script by putting at the bottom of the python script this sort of code (actually a python comment):
# Trigger emacs to run this script using the "compile" command
# ;;; Local Variables: ***
# ;;; compile-command: "my_cool_script.py --complicated_option some_filename.txt" ***
# ;;; end: ***
This is handy for scripts with complicated invocations.
Note: The python comment character '#' protects this from the python interpreter while Emacs knows to set these variables because it looks at the bottom of every file when it opens them.
I'd love to be able to jump to 'compile errors' in my python script the way the compile command does when you use it for compiling C code but I'm too lazy to create the Emacs regular expression to make this work. Perhaps that would make another great question for stack overflow!

I currently use these hook to define my compilation commands:
(defun convert-filename-to-executable (file)
(if (eq system-type 'windows-nt)
(concat (file-name-sans-extension file) ".exe")
;; linux
(concat "./" (file-name-sans-extension file))))
(add-hook 'c++-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer- file-name))
(executable (convert-filename-to-executable file)))
(concat "g++ -g -Wall -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'c-mode-hook
(lambda ()
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let* ((file (file-name-nondirectory buffer-file-name))
(executable (convert-filename-to-executable file)))
(concat "gcc -g -ansi -Wall -Wpedantic -Wextra -Wc++-compat -Wconversion -o "
(file-name-sans-extension file)
" "
file
" && "
executable))))))
(add-hook 'python-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
(add-hook 'perl-mode-hook
(lambda ()
(set (make-local-variable 'compile-command)
(concat "python " buffer-file-name))))
Together with this lambda set to call the compile function interactively:
(global-set-key (kbd "<f4>") (lambda () (interactive) (setq current-prefix-arg '(4)) (call-interactively 'compile)))
One button to rule them all!
If you press F4 (in my case, you can set the key yourself in the lambda for global-set-key), then a file opened in C++ or C mode will be compiled and a file in python or perl mode will be run (interactively)

The I think ansi-term has the most faithful emulation of a terminal. But I don't see a way to pass arguments to the process. You can of course just launch it from a shell inside the ansi-term buffer.
But I think the best thing to do is to not use python-send-buffer, but instead to use a new function which does it "right", that is by sending the path to the current file instead of making a temp file. There are some slight differences of course in that you have to save the current file first, but the following should at least get you on the right track.
(defun python-send-file ()
(interactive)
(save-buffer)
(python-send-string (concat "execfile('" (buffer-file-name) "')")))
;; This overwrites the `python-send-buffer' binding so you may want to pick another key
(eval-after-load "python"
(define-key python-mode-map "\C-c\C-c" 'python-send-file))
I checked and this allows you to interact. To get tabs you have a few options.
C-qTAB will always give you a literal tab
You can rebind tab to be a literal tab in inferior-python-mode-map:
(define-key inferior-python-mode-map "\C-i" 'self-insert-command)
I'm sure there are others that I can't think of

If you use C-c C-c a buffer is created (look for inferior-python). Try changing to that buffer*, every time you hit C-c C-c the result is shown there, you need to see that buffer to get the results. Use C-x 2 so you can see both buffers at the same time.
Also try C-c C-z (switch to shell).
*I use Ibuffer to manage buffers, is very good.
(btw, this http://tuhdo.github.io/index.html is an excelent place to learn some emacs)
EDIT: Have you tried C-c C-p ?

Yet another option:
Using C-c C-c works nicely with fgallina's python.el -- pwd will be the location of the buffer's file.

Related

asynchronously evaluating whole python or perl buffer

I've written some functions attached to my F5 and F6 keys so that when pressed, these evaluate the buffer contents for perl or python.
;; F5 = perlevaluatie
(defun perl-eval ()
"Run whole buffer as Perl code"
(interactive)
(shell-command-on-region (point-min) (point-max) "perl") ; feeds the region to perl on STDIN
)
(global-set-key (kbd "<f5>") 'perl-eval)
;; F6 = pythonevaluatie
(defun python-eval ()
"Run whole buffer as Python code"
(interactive)
(shell-command-on-region (point-min) (point-max) "python")
)
(global-set-key (kbd "<f6>") 'python-eval)
However, when I use these functions for a script that keeps running for long times, emacs hangs. Appending an & does not help as with the shell-command function. Does anybody know a way to make the shell-command-on-region asynchronous?
Thanks in advance,
Marten
I use compile for this: M-x compile, perl <file-name>. After the first run you can use recompile to rerun it.
Slightly more time to type the file's name but you gain a lot of nice stuff, and it's asynchronous of course.
Edit: Bonus helper function to automatically use compile the first time then recompile:
(defun my-recompile ()
"Recompile if possible, otherwise compile current buffer."
(interactive)
;; If recompile exists do it, else compile
(if (fboundp 'recompile) (recompile)
(compile "make -k")))
and in the appropriate hook bind something like
(local-set-key (kbd "<f5>") 'my-recompile)
(local-set-key (kbd "<C-f5>") 'compile)

Store `local-set-key` in the configuration of emacs

For example, I use the command local-set-key to set the key C-c C-n for flymake-goto-next-error in python-mode.
Instead of writing an expression and wrapping it into python-mode-hook, is there a convenient way to store this keybinding directly? Does anyone have ideas about this?
Command
(local-set-key (kbd "C-c C-n") 'flymake-goto-next-error)
works in the current buffer's local map. It is correct when local map is python-mode-map.
Convenient minimum lenght command (without using hook)
(define-key python-mode-map (kbd "C-c C-n") 'flymake-goto-next-error)
works when variable python-mode-map has been created.
Variable python-mode-map is created dynamically (in file python-mode.el) after call
(require 'python-mode)
If python-mode hasn't been loaded, command define-key can't directly use python-mode-map. Using hooks is for safety. Your emacs config should be reliable (it shouldn't depend on command's execution order if possible), and such wrapped (with hooks) commands prevent wrong situation: setting keybind in mode-map without setup mode.

aquamacs: windows swapping on C-c C-c (python mode)

I apologize if I use the incorrect terminology here, I've only been using emacs for a few months.
I just reinstalled Aquamacs on a macbook that I reformatted and am having the oddest problem.
I open a .py file, and use C-c ! to open a python shell. So I have (as expected), the .py file in the top window and the python shell in the bottom window.
If I then run C-c C-c (py-execute-buffer) in the .py file, the two windows swap positions. I mean, the .py file buffer opens in the bottom window in a new buffer, while the python shell opens in the top window in a new buffer. So basically, they swap positions. Repeatedly using C-c C-c swaps the windows back again... so they're shuffling positions. Also, both windows (top and bottom) have both buffers (.py file and python shell) in tabs.
I've not made any modifications to the default settings yet, and I've gotten the problem with both 2.3a and 2.3 (2.3 was on the machine previously and didn't have this problem, so I tried rolling back... to no avail).
Does anyone know how to stop this behavior? Thanks in advance!
Add the following to your Emacs init file in Aquamacs to prevent it from swapping the buffers around:
(defadvice py-execute-buffer
(around keep-buffers-same activate)
"Don't swap buffers in Aquamacs."
(save-window-excursion
ad-do-it))
You can also try adding the following to your emacs init file:
(setq py-split-windows-on-execute-p nil)
This will prevent the current window from splitting after you run any py-execute-*. (This also means that the python shell won't show up if it isn't already in one of your windows.)
i don't use Aquamacs and couldn't reproduce your described behaviour, however, try this code to toggle either of the windows as 'dedicated'. locking windows to buffers was the first thing i wanted to do when getting up and running with emacs. maybe this will help you here.
add the code to your '.emacs', then either 'mark' (select) the region 'S-< key-down >' and then 'M-x eval-region' to evaluate it ..or save and restart emacs.
(global-set-key [pause] 'window-dedication-toggle)
(defun window-dedication-toggle (&optional window force quiet)
"toggle or ensure the 'dedicated' state for a window"
(interactive)
(let* ((toggle t) (window (if window window (selected-window))) (dedicated (window-dedicated-p window)))
(cond ((equal force "on") (setq toggle (eq dedicated nil)))
((equal force "off") (setq toggle (eq dedicated t))))
(if toggle (progn
(setq dedicated (not dedicated))
(set-window-dedicated-p window dedicated)
(if (not quiet)
(message "window %sdedicated to %s" (if (not dedicated) "no longer " "") (buffer-name)))))))

evaluating buffer in emacs python-mode on remote host

I'm using emacs23 with tramp to modify python scripts on a remote host.
I found that when I start the python shell within emacs it starts up
python on the remote host.
My problem is that when I then try to call python-send-buffer via C-c C-c it comes up with the error
Traceback (most recent call last):
File "", line 1, in ?
ImportError: No module named emacs
Traceback (most recent call last):
File "", line 1, in ?
NameError: name 'emacs' is not defined
Now, I must admit that I don't really know what's going on here. Is there a way for me to configure emacs so that I can evaluate the buffer on the remote host?
Many thanks.
Edit: I've followed eichin's advice and re-implemented python-send-region. See my answer below.
I'm currently trying to to get my unregistered question merged with this account, after which I'll be able to accept eichin's answer and edit my post to include my solution.
I followed eichin's suggestion and copied the emacs2.py emacs3.py and emacs.py files to the remote host and added their directory to PYTHONPATH in the tramp-remote-process-environment variable.
I then reimplemented the python-send-buffer function in my .emacs
(require 'python)
(defun python-send-region (start end)
"Send the region to the inferior Python process."
(interactive "r")
(let* ((loc_name)
(f (if (file-remote-p default-directory)
(let* ((con (tramp-dissect-file-name default-directory)))
(setq loc_name (tramp-make-tramp-temp-file con))
(concat "/"
(tramp-file-name-method con) ":"
(tramp-file-name-user con) "#"
(tramp-file-name-host con) ":"
loc_name
))
(setq loc_name (make-temp-file "py"))))
(command (format "emacs.eexecfile(%S)" loc_name))
(orig-start (copy-marker start)))
(save-excursion
(let ((curbuf (current-buffer))
(tempbuf (get-buffer-create "*python_temp*")))
(set-buffer tempbuf)
(delete-region (point-min) (point-max))
(insert-buffer-substring curbuf start end)
(python-mode)
(when (save-excursion
(goto-char (point-min))
(/= 0 (current-indentation)))
(python-shift-left (point-min) (point-max)))
(write-region nil nil f nil 'nomsg))
(python-send-command command)
(with-current-buffer (process-buffer (python-proc))
;; Tell compile.el to redirect error locations in file `f' to
;; positions past marker `orig-start'. It has to be done *after*
;; `python-send-command''s call to `compilation-forget-errors'.
(compilation-fake-loc orig-start f)))
))
I essentially copy the region into a new buffer, adjust the indentation and then write it into a temporary file, created with tramp-make-tramp-temp-file or make-temp-file, depending on whether the visited file is remote or local.
I had some problems with tramp-handle-write-region, which didn't seem to accept a string as a first argument, which is why I did all the formatting in a separate buffer first.
Let me know if there are still any problems with the code, but this is my first attempt at elisp coding, so please be gentle.
Short answer: not without writing some missing elisp code.
Long version: In python.el, run-python adds data-directory (which on my Ubuntu 10.10 box is /usr/share/emacs/23.1/etc/ ) to $PYTHONPATH, specifically so that it can find emacs.py (as supplied by the local emacs distribution.) Then it does a (python-send-string "import emacs") and expects it to work...
It looks like the defadvice wrappers that tramp uses don't actually pass PYTHONPATH, so this doesn't work even if you have the matching emacs version on the remote system.
If you M-x customize-variable RET tramp-remote-process-environment RET
then hit one of the INS buttons and add PYTHONPATH=/usr/share/emacs/23.1/etc then hit STATE and set it to "current session" (just to test it, or "save for future sessions" if it works for you) it almost works - the complaint goes away, in any case, because the remote python can now find the remote emacs.py. If you now go back to the original question, doing python-send-buffer, you just run into a different error: No such file or directory: '/tmp/py24574XdA' because python-mode just stuffs the content into a temporary file and tells the python subprocess to load that.
You'd have to change python-send-region (the other functions call it) and particularly the way it uses make-temp-file to be tramp-aware - there's even a tramp-make-tramp-temp-file you could probably build upon. (Be sure to post it if you do...)

Emacs: Set/Reset python debug breakpoint

I use python debugger pdb. I use emacs for python programming. I use python-mode.el. My idea is to make emacs intuitive. So I need the following help for python programs (.py)
Whenever I press 'F9' key, the emacs should put "import pdb; pdb.set_trace();" statements in the current line and move the current line to one line below.
Sentence to be in same line. smart indentation may help very much.
Wherever "import pdb; pdb.set_trace();" statement presents in the python code, emacs should display left indicator and highlight that line.
When I press 'Alt-F9' keys at the current line and emacs found the "import pdb; pdb.set_trace();" statement then, emacs should remove the "import pdb; pdb.set_trace();" line and move the current line to one up.
Whenever I press "F8" key, emacs to jump to "import pdb; pdb.set_trace();" in the same buffer.
I am trying to learn elisp and catch up lisp soon to customize emacs myself. I will appreciate your answers.
The answer shall be great enough for me and others who find this solution is very useful.
to do 1)
(defun add-py-debug ()
"add debug code and move line down"
(interactive)
(move-beginning-of-line 1)
(insert "import pdb; pdb.set_trace();\n"))
(local-set-key (kbd "<f9>") 'add-py-debug)
to do 2) you probably have to change the syntax highlighting of the python mode, or write you own minor mode. You'd have to look into font-lock to get more. Sorry.
to do 3) though I've set this to be C-c F9 instead of Alt-F9
(defun remove-py-debug ()
"remove py debug code, if found"
(interactive)
(let ((x (line-number-at-pos))
(cur (point)))
(search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")
(if (= x (line-number-at-pos))
(let ()
(move-beginning-of-line 1)
(kill-line 1)
(move-beginning-of-line 1))
(goto-char cur))))
(local-set-key (kbd "C c <f9>") 'remove-py-debug)
and to do 4)
(local-set-key (kbd "<f3>") '(lambda ()
(interactive)
(search-forward-regexp "^[ ]*import pdb; pdb.set_trace();")
(move-beginning-of-line 1)))
Note, this is not the best elisp code in the world, but I've tried to make it clear to you what's going on rather than make it totally idiomatic. The GNU Elsip book is a great place to start if you want to do more with elisp.
HTH
I've found that Xah's Elisp Tutorial is an excellent starting point in figuring out the basics of Emacs Lisp programming. There are also some SteveY articles from a while ago that go through techniques you might find useful for learning the basics.
If you're serious about making an amended Python mode, you'll do well to take a look at Writing GNU Emacs Extensions, which is available as a PDF.
Finally, the most useful resource for me is actually Emacs itself. I make frequent use of M-x apropos and M-x describe-key to figure out how built-in functions work, and whether there's something already in place to do what I want.
The specific things you want to look like they can be done through some simple use of insert, and a few search/replace functions, so that'll be a good starting point.

Categories