4

When I try to start a Python shell in Emacs 24.4.1 running on Windows, I get an error "invalid argument" when Emacs tries to spawn the process. This is occurring because Python is installed under c:\Program Files (x86), which of course has those nasty embedded spaces. When I try to run Python from Emacs, it gets the path right in the prompt, but then the call fails because the spaces cause the command to look like there are multiple arguments. I can manually add quotes around the path in the mini-buffer prompt, but that's obviously not a solution. Oddly enough, Emacs 23.2.1 on the same machine has no problem with invoking Python.

I'd prefer not to change the location of Python if I don't have to.

  • Sounds like a bug on whichever package you're using. Emacs doesn't usually have problems with executables with spaces in their names. – Malabarba Dec 5 '14 at 14:41
  • The problem stems from some change in the python.el package. If I remove the version of python.el that was bundled with the pre-built Windows version of 24.4.1 and replace it with the version bundled with 23.2.1, I can create the Python shell without the "invalid argument" error. However, the shell itself now has an error at startup - "No module named emacs". Unfortunately there are substantial differences between the two versions of python.el, so tracking down why the embedded spaces are screwing things up is not a simple task. – Llaves Dec 6 '14 at 3:40
4

I think I've found the problem and a work-around. If you look at the python.el file, you will find this function to determine the path to the python executable:

  (defun python-shell-parse-command ()    ;FIXME: why name it "parse"?
  "Calculate the string used to execute the inferior Python process."
  ;; FIXME: process-environment doesn't seem to be used anywhere within
  ;; this let.
  (let ((process-environment (python-shell-calculate-process-environment))
        (exec-path (python-shell-calculate-exec-path)))
    (format "%s %s"
            ;; FIXME: Why executable-find?
            (executable-find python-shell-interpreter)
            python-shell-interpreter-args)))

(BTW - the FIXME notes are in the code - they are not my comments)

Anyway, the problem is the executable-find returns an unquoted fully defined path, which in turn is passed to the shell and creates the "invalid argument" error. executable-find (defined in files.el) in turn calls locate-file which has the same problematical behavior for paths with embedded spaces. The real fix is probably to correct locate-file, but I'm not at all familiar with the emacs code base to even remotely guess at the broader consequences of changing this. Perhaps there should also be a check if the code is running on a Windows system. I really don't know.

So, here's the workaround. Change the format statement to add the quotes you need

   (format "\"%s\" %s"

Now the shell opens error-free.

UPDATE: The emacs team replied to my bug report pointing out the correct way to fix this is with the function shell-quote-argument which handles the differences between OSs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.