Mac – ERROR: AUCTeX cannot find a working TeX distribution (macOS Sierra)

emacspath

I have installed MacTex and GNU Emacs on my iMac. I have created a simple tex file (the one suggested here) and tried to run LaTeX on it by hitting C-c C-c, but the following message appeared:

ERROR: AUCTeX cannot find a working TeX distribution.
Make sure you have one and that TeX binaries are in PATH environment variable.
If you are using OS X El Capitan or later
remember to add /Library/TeX/texbin/ to your PATH

Opening Terminal and running

echo $PATH

yields

/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin


Operating system: macOS Sierra, Version 10.12.4
Emacs version: 25.1.1
AUCTeX version: 11.90.0
MacTex distribution: MacTeX-2016

Best Answer

As patrix suggested, the problem was that the PATH variable that the error message referred to was not the Terminal's environment variable but emacs' own environment variable, whose content can be seen (as suggested by patrix) by evaluating (getenv "PATH") in Emacs.

To add /Library/TeX/texbin/ to emacs' PATH variable, I added the following code to the emacs initialization file .emacs:

(setenv "PATH" (concat (getenv "PATH") ":/Library/TeX/texbin/"))  
(setq exec-path (append exec-path '("/Library/TeX/texbin/")))

These lines of code are appropriately modified versions of the ones given here.

After restarting Emacs, the problem was gone.