evaluating buffer in emacs python-mode on remote host - python

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...)

Related

Calling Python (anaconda) with Batch File from ASP.NET

I am trying to call a python script from my asp.net web app. I am using a batch file to call the python script. The python script runs from the batch file when I run it on the server. When I try to call the batch file from asp.net, the code in the batch file works until it gets to the call for the python, and the call to the python script doesn't work.
This is my batch file:
(
echo Hello
)
C:\inetpub\Sites\**\**\**\test.txt
rem the code above works from ASP.NET
rem this code is never called
set CONDAPATH="C:\\Users\\***\\Anaconda3"
set ENVNAME="base"
if %ENVNAME%==base (set ENVPATH=%CONDAPATH%) else (set ENVPATH=%CONDAPATH%\envs\%ENVNAME%)
call %CONDAPATH%\Scripts\activate.bat %ENVPATH%
C:\\Users\\***\\Anaconda3\\python.exe C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py
Could anyone help me figure out what I am doing wrong in calling this python code?
The first thing you need to understand is that your line:
C:\inetpub\Sites\**\**\**\test.txt
will open a text file in the default application for doing so, and your script will then wait until you have closed that text file, before it continues to the next line. If you want the code to continue without waiting for the text file to be closed, you can use the start command:
Start "" "C:\inetpub\Sites\**\**\**\test.txt"
Here's a quick example to demonstrate my earlier comment, regarding the recommended syntax for defining variables, and using doublequotes around file paths.
Set "CONDAPATH=%UserProfile%\Anaconda3"
Set "ENVNAME=base"
If /I "%ENVNAME%" == "base" (Set "ENVPATH=%CONDAPATH%") Else Set "ENVPATH=%CONDAPATH%\envs\%ENVNAME%"
Call "%CONDAPATH%\Scripts\activate.bat" "%ENVPATH%"
"%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
Putting it all together:
#Echo Off
Echo Hello
Start "" "C:\inetpub\Sites\**\**\**\test.txt"
Set "CONDAPATH=%UserProfile%\Anaconda3"
Set "ENVNAME=base"
If /I "%ENVNAME%" == "base" (Set "ENVPATH=%CONDAPATH%") Else Set "ENVPATH=%CONDAPATH%\envs\%ENVNAME%"
Call "%CONDAPATH%\Scripts\activate.bat" "%ENVPATH%"
"%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
If you don't want the script to wait until the python command has completed, (for instance, if this is the last line of your script), you could change that last line to:
Start "" "%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
I figured it out in case anyone is still using asp.net like me. It was all a permissions issue on my folder that had I was calling the python script from CONDAPATH="C:\Users\***\Anaconda3".
This link gave me the hint: IIS7 Permissions Overview - ApplicationPoolIdentity

emacs `python-shell-send-defun` skips first line in buffer

There appears to be a pretty significant bug in C-M-x, a.k.a. python-shell-send-defun in the builtin python mode
start emacs
create a new file, let's call it test.py
C-c C-p to run-python
in the test.py buffer, type some simple function, like
def f(x):
return x+1
C-M-x for python-shell-send-defun
we get:
return x+1
SyntaxError: 'return' outside function
So what's happening here is that python-shell-send-defun is only sending one line, not both lines as we would expect it to (the corresponding command in emacs lisp major mode for instance works correctly in sending the entire multiline defun prior to the cursor). What's going on here? I tested this in both emacs 25 gui version in Mac OS as well as emacs 24 in Ubuntu. Both exhibit the same error.
Is this a bug, or am I misunderstanding what python-shell-send-defun actually does?
Update: with some further testing, it appears that this error only occurs for functions that start on the very first line. If we insert a blank line right above def f(x):, C-M-x works perfectly.
Yes, this looks like a bug and should be reported as one. That function should probably be rewritten to have a check as follows
(defun python-shell-send-defun (&optional arg msg)
"Send the current defun to inferior Python process.
When argument ARG is non-nil do not include decorators. When
optional argument MSG is non-nil, forces display of a
user-friendly message if there's no process running; defaults to
t when called interactively."
(interactive (list current-prefix-arg t))
(save-excursion
(python-shell-send-region
(progn
(end-of-line 1)
(while (and (or (python-nav-beginning-of-defun)
(beginning-of-line 1))
(> (current-indentation) 0)))
(when (not arg)
(while (and (forward-line -1)
(looking-at (python-rx decorator))))
;; add a check here to see if we are on the first line
(and (not (bobp)) (forward-line 1)))
(point-marker))
(progn
(or (python-nav-end-of-defun)
(end-of-line 1))
(point-marker))
nil ;; noop
msg)))

Is there a function like eval-print-last-sexp for Comint-Mode?

Is there an equivalent function to eval-print-last-sexp for comint derived modes in emacs?
Specifically, I'm using python-mode (with elpy) and am looking for a way to send the contents of a region to the Python process and then print the results on the next line of the python script I'm working in.
Printing the results to the Messages buffer would be acceptable as well, however the behavior of eval-print-last-sexp would be preferred.
I'm running Emacs 25.1.1 and elpy 1.13.0.
It would depend on the comint-derived mode, since you would need to redirect the process output. Different modes have different methods of interacting with inferior processes. Python mode already has a function to do this, python-shell-send-string-no-output (other modes have similar functions, but you would need to search for them).
I'm not sure how exactly you want to define a sexp for python, but here is an example of sending the current line, with output like eval-print-last-sexp.
(defun python-eval-print-last-sexp ()
"Print result of evaluating current line into current buffer."
(interactive)
(let ((res (python-shell-send-string-no-output
;; modify to get a different sexp
(buffer-substring (line-beginning-position)
(line-end-position))))
(standard-output (current-buffer)))
(when res
(terpri)
(princ res)
(terpri))))
elpy-shell-send-statement from the Emacs Python Development Environment.

Running interactive python script from emacs

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.

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)))))))

Categories