40

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?

Calaf
  • 10,113
  • 15
  • 57
  • 120

4 Answers4

72

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.

sanityinc
  • 15,002
  • 2
  • 49
  • 43
  • 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? – Calaf Dec 22 '11 at 22:19
  • Ah, yes, it's my own function: I'll edit the answer to fix this. – sanityinc Dec 23 '11 at 08:35
  • 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. – Calaf May 08 '12 at 15:16
  • 2
    @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. – sanityinc May 08 '12 at 18:39
  • 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. – Calaf May 10 '12 at 20:47
  • Yes, but as you can see above, I edited the answer in December to remove the use of that function. :-) – sanityinc May 12 '12 at 06:16
  • 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. – Calaf May 17 '12 at 18:38
  • 2
    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`. – sanityinc May 17 '12 at 20:23
  • @sanityinc It is conventional for exec-directory to be the final element of exec-path. Maybe add that to your code? – hruvulum Feb 18 '14 at 05:19
  • @hruvulum Thanks for the pointer -- the "polished" version of this code now does exactly that: https://github.com/purcell/exec-path-from-shell – sanityinc Feb 19 '14 at 09:06
  • 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? – Zelphir Kaltstahl Feb 11 '18 at 17:59
  • 1
    @Zelphir `exec-path-from-shell` normally sets `eshell-path-env`. If you want help, feel free to file an issue on the [repo](https://github.com/purcell/exec-path-from-shell). – sanityinc Feb 13 '18 at 08:40
  • 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) – xdavidliu Mar 14 '18 at 21:11
  • `open /Applications/Emacs.app/` – MarcH Feb 04 '20 at 22:05
11

Try replacing the second line with this:

(setq exec-path (append exec-path '("/usr/texbin" "/opt/local/bin")))
huaiyuan
  • 26,129
  • 5
  • 57
  • 63
  • 1
    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 Dec 22 '11 at 17:19
1

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").
chollida
  • 7,834
  • 11
  • 55
  • 85
Hanz
  • 11
  • 1
-3

It appears you're missing a path separator : at the end of your path string.

event_jr
  • 17,467
  • 4
  • 47
  • 62