PATH and exec-path set, but emacs does not find executable

27,910

Solution 1

If you're setting $PATH inside your Emacs, you might well be on OS X. GUI applications are not started via your shell, so they see different environment variables.

Here's a trick which I use to ensure the $PATH inside Emacs is the same one I see if I fire up a terminal (but see "update" below):

(defun set-exec-path-from-shell-PATH ()
  "Set up Emacs' `exec-path' and PATH environment variable to match that used by the user's shell.

This is particularly useful under Mac OSX, where GUI apps are not started from a shell."
  (interactive)
  (let ((path-from-shell (replace-regexp-in-string "[ \t\n]*$" "" (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'"))))
    (setenv "PATH" path-from-shell)
    (setq exec-path (split-string path-from-shell path-separator))))

Then simply call the set-exec-path-from-shell-PATH function, perhaps from your Emacs init file. I keep that code on github, BTW.

Update: this code has now been improved and published as an elisp library called exec-path-from-shell; installable packages are available in MELPA.

Solution 2

Try replacing the second line with this:

(setq exec-path (append exec-path '("/usr/texbin" "/opt/local/bin")))

Solution 3

I hit a similar problem, but with a correct PATH, including trailing ´:´. It turned out the internal emacs shell program was missing, resulting in a ´Searching for program: No such file or directory´ message. Fixed with

(setq shell-file-name "bash").
Share:
27,910
Calaf
Author by

Calaf

Updated on March 24, 2020

Comments

  • Calaf
    Calaf over 4 years

    My .emacs contains

    (setenv "PATH" (concat ".:/usr/texbin:/opt/local/bin" (getenv "PATH")))
    (setq exec-path (append exec-path '(".:/usr/texbin:/opt/local/bin")))
    
    (add-to-list 'load-path "/usr/local/share/emacs/site-lisp")
    (require 'tex-site)
    (load "auctex.el" nil t t)
    (load "preview-latex.el" nil t t)
    

    /usr/texbin is where latex/pdflatex/.. are located. /opt/local/bin/ is where gs can be found.

    And yet when I run preview-at-point, which apparently needs both latex and gs, I get

    Preview-DviPS finished at Thu Dec 22 11:25:46
    DviPS sentinel: Searching for program: No such file or directory, gs
    

    which means that latex could be found all right, but not gs.

    I am not sure whether setting exec-path is necessary, perhaps PATH is enough, but I've set it as a debugging measure.

    Why can emacs not find gs even though the directory it's in is in both PATH and exec-path?

  • Drew
    Drew over 12 years
    IOW, use ".:/usr/texbin:/opt/local/bin:" (note : at end).
  • Calaf
    Calaf over 12 years
    After adding a terminating path separator (and restarting emacs) I still get the same error.
  • Calaf
    Calaf over 12 years
    That solves the gs problem, but there is now another problem, which seems related to the permissions of the file generated by gs. Let me ask that question separately.
  • Calaf
    Calaf over 12 years
    I am indeed on OSX, and loading PATH and exec-path from the shell would be very nice. Yet string-rtrim is not found (by neither emacs 22 nor 23). Is it your own function?
  • sanityinc
    sanityinc over 12 years
    Ah, yes, it's my own function: I'll edit the answer to fix this.
  • Calaf
    Calaf about 12 years
    Greetings.. If you have a chance, please update your answer. It would be very handy for us emacs/Mac users. As you identified, the question is really relevant mostly to Mac folks, so if you have enough points, please migrate the queston to apple.stackexchange.com.
  • sanityinc
    sanityinc about 12 years
    @Calaf The answer had already been updated as promised. I don't get involved with the other stackexchange sites, and the correct home for Emacs questions is a perenially contentious topic, so I'll decline to move the question.
  • Calaf
    Calaf about 12 years
    No problem about moving the answer. I personally prefer a single site with osx as a tag anyway, though the current convention is otherwise. About the answer: it is not usable because string-rtrim is undefined, as I mentioned earlier in this thread.
  • sanityinc
    sanityinc about 12 years
    Yes, but as you can see above, I edited the answer in December to remove the use of that function. :-)
  • Calaf
    Calaf about 12 years
    I see. Thanks. I had missed the update. Can you explain something? The standard place for updating the PATH env variable is in .bash_profile. MacPorts, for one, updates that file directly. But .bash_profile is not run before emacs is launched, and so any modifications to PATH there are missed.
  • sanityinc
    sanityinc about 12 years
    Yes. GUI apps are not launched via your shell, so they don't see any of the shell's environment settings. If you start Emacs by running /Applications/Emacs.app/Contents/MacOS/Emacs from your shell, it will probably see the correct $PATH. OSX uses an environment.plist file to determine the environment vars seen by GUI apps, but my method above avoids the need to edit both that file and .bash_profile.
  • hruvulum
    hruvulum over 10 years
    @sanityinc It is conventional for exec-directory to be the final element of exec-path. Maybe add that to your code?
  • sanityinc
    sanityinc over 10 years
    @hruvulum Thanks for the pointer -- the "polished" version of this code now does exactly that: github.com/purcell/exec-path-from-shell
  • Thomas
    Thomas over 8 years
    No! Exactly don't do this. This is for shell PATH's not for the exec-path variable which is just a list.
  • Zelphir Kaltstahl
    Zelphir Kaltstahl over 6 years
    The package exec-path-from-shell adds all the right things to exec-path, but eshell still does not find executables or executable scripts from those directories. GNU Emacs 24.5.1 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) What more needs to be done?
  • sanityinc
    sanityinc over 6 years
    @Zelphir exec-path-from-shell normally sets eshell-path-env. If you want help, feel free to file an issue on the repo.
  • xdavidliu
    xdavidliu over 6 years
    Mac OS X user here: I personally find infuriating this gotcha of GUI applications not having the same $PATH as terminal. I have been painstakingly manually setting the path-to-shell variable for every single inferior shell I'm using in emacs, assuming that emacs not capturing my $PATH was just a feature, not a bug (though looking back, I do seem to recall not having to do this on Ubuntu)
  • MarcH
    MarcH over 4 years
    open /Applications/Emacs.app/