If I use tramp to open a remote file, and subsequently run
run-python, it tries to execute in the remote context (which is not what I want and super slow).
Any way of specifying a local context or disabling this behaviour?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
(defun run-python-locally (&rest args) (interactive (progn (require 'nadvice) (advice-eval-interactive-spec (cadr (interactive-form #'run-python))))) (let ((default-directory user-emacs-directory)) (apply #'run-python args)))
(This uses the trick from this answer).
Additionally, if you'd like to use
python-shell-send-region and company, you'll need this fix:
(eval-when-compile (require 'cl-lib)) (defun nadvice/python-shell-send-string/fix-local-process (old-fun string &optional process) (cl-letf ((old-psstf (symbol-function #'python-shell--save-temp-file)) ((symbol-function #'python-shell--save-temp-file) (lambda (string) (let ((default-directory ;; if buffer is a remote file, but the process is not ;; save the temp file locally, instead of remotely (if (and buffer-file-name (file-remote-p buffer-file-name) (not (plist-get 'remote-tty (process-plist process)))) user-emacs-directory default-directory))) (funcall old-psstf string))))) (funcall old-fun string process))) (advice-add 'python-shell-send-string :around #'nadvice/python-shell-send-string/fix-local-process)
You can start a local python console by executing
run-python while in a local buffer (
*scratch* will do). The problem is that python interaction will not work for remote files then without some additional hacking.
Generally you need the python process to be running on the same machine that the files reside on
since emacs communicates with the python process vial the file system.
python-shell-send-buffer saves a copy of the buffer in /tmp for the process to read.
With a remote file and a local python process the buffer will get saved to on the remote machine and python will try to read from the same path on the local one and fail to find it.
To make python interaction work with files on a different machine from where the python shell is running would require tweaking the code to put temporary files on the machine where the python is running, regardless of where the file is, or where you are running emacs. I would be interested to know how to do that myself.