5
$\begingroup$

Under macOS Sierra, at least, where does Mathematica (currently 11.2) get the value of Environment["PATH"]?

A comment to the accepted answer at Why does Mathematica use a different $PATH than terminal? seems to claim that Mathematica gets that value from ~/.bash_profile along with /etc/path and the files in /etc/paths.d. That is manifestly wrong!

In fact, on my system,

Environment["PATH"]

gives output only:

/usr/bin:/bin:/usr/sbin:/sbin

However, /etc/path includes the additional path /usr/local/bin, and /etc/paths.d includes among others the file TeX that contains the additional path /Library/TeX/texbin.

[I do not have any file ~/.bash_profile, as that would preclude essential use of ~/.profile, and the latter is where various applications, including MacPorts, append to the path. (My understanding is that if ~/.bash_profile exists, then at least in Terminal, ~/.profile would never be read.)

$\endgroup$
3
  • $\begingroup$ Mathematica inherits the environment from the process that starts it, just like other apps. If it is started from a terminal, it will have the value you set in the terminal (i.e. what's in your .profile). Otherwise, it will have the default system value, which isn't affected by .profile. $\endgroup$
    – Szabolcs
    Sep 24, 2017 at 17:15
  • $\begingroup$ As for setting the system default, I think this is the relevant post: apple.stackexchange.com/q/107787/31058 But I have never done this. $\endgroup$
    – Szabolcs
    Sep 24, 2017 at 17:17
  • $\begingroup$ I'm always starting Mathematica.app directly from Finder, and not using a command in Terminal (except on rare occasions when I use the wolframscript command-line executable). $\endgroup$
    – murray
    Sep 25, 2017 at 18:20

2 Answers 2

2
$\begingroup$

As noted in the second answer to the question you linked to, this is not entirely stable from version to version in OS X. That's under Apple's control, not ours. It also varies based on the specifics of your shell and configuration. For example, I use zsh as my shell and set the path in .zshenv rather than .profile or .zprofile. I don't think that bash has a similar mechanism.

If you wish to ensure that Mathematica's path is identical to the Terminal's, you can launch it from the terminal by typing /Applications/Mathematica/Contents/MacOS/Mathematica &. It will then inherit the value of PATH from your terminal rather than from Launch Services. If you double click on Mathmeatica (or, equivalently, type open /Applications/Mathematica.app) then Mathematica inherits from the graphical environment, which may be different.

$\endgroup$
4
  • $\begingroup$ One could set up a new default kernel which is started through a shell script that explicitly reads the .profile. On OS X this will work transparently. $\endgroup$
    – Szabolcs
    Sep 24, 2017 at 17:22
  • $\begingroup$ @Szabolcs: If I understand correctly, all this involves is to specify that shell script in the "Launch Command" field of the Kernel Configuration dialog window that opens from menu Evaluation > Kernel Configuration Options > Add and then to make that the Default Kernel. Is that what you meant? $\endgroup$
    – murray
    Sep 27, 2017 at 14:04
  • 1
    $\begingroup$ @murray Yes. Somewhat inconvenient, but at least it's a one-time inconvenience, and much better than starting from the command line. I saw your comments on Wolfram Community after I wrote this answer. I think the solution for that would be for J.M. to update the package, so you can configure where to look for the executable (instead of just finding it on the path). $\endgroup$
    – Szabolcs
    Sep 27, 2017 at 17:26
  • $\begingroup$ @Szabolcs, sounds like a good idea. But it would be nice to get feedback from other Mac users first so I could get a handle on what to do here, since I can't test on a Mac. $\endgroup$ Oct 1, 2017 at 1:42
1
$\begingroup$

@Itai explained why Mathematica doesn't pick up .profile.

As a simple way to pick up your .profile, you can create a shell script for starting up Mathematica, and explicitly read .profile in it. For example:

#!/bin/sh

source ~/.profile
/Applications/Mathematica\ 11.2.app/Contents/MacOS/MathKernel "$@"

Adjust the path to MathKernel on your system. Make the script executable (e.g. from the terminal chmod a+x yourscript.sh)

Then create a new kernel in Evaluation -> Kernel Configuration Options, and simply replace WolframKernel or MathKernel with the full path to this script.

$\endgroup$
6
  • $\begingroup$ I do not need to pick up the entire path from .profile that Terminal uses! I merely want to pick up one more location, namely, opt/local/bin. And in any case I do not want to have to start Mathematica.app in any but the usual ways from Finder, including clicking on its icon in the Dock. $\endgroup$
    – murray
    Sep 25, 2017 at 18:23
  • $\begingroup$ @murray I don't understand your comment. I just showed you how to set any path you want without having to start Mathematica from the terminal ... Set up the kernel to be started through a shell script that adjusts the path as you need. No terminal needed. $\endgroup$
    – Szabolcs
    Sep 26, 2017 at 7:49
  • $\begingroup$ OK, I see now what to do, viz., just replace ~/.profile by another file that contains my desired path. Will try. Thanks. (Still annoying, and puzzling, that Mathematica does not pick up the entire path as specified in /etc/paths and /etc/paths.d/*, or at least in /etc/paths.) $\endgroup$
    – murray
    Sep 28, 2017 at 14:00
  • $\begingroup$ This is not working for me. The shell script you show (edited to replace Mathematica\ 11.2.app to just Mathematica.app, the name on my system), is OK, as I can run it directly from a Terminal command line and thereby start a kernel. (continued in separate comment) $\endgroup$
    – murray
    Sep 28, 2017 at 16:35
  • $\begingroup$ (continuation) But if in Mathematica` Evaluation > Kernel Configuration Options...` I uncheck "Automatically launch on from end startup" the (default) Local kernel, add a new kernel KernelMyPath, in the Edit window for that click "Advanced Options", and in the "Launch command" field insert ~/bin/mma_with_path.sh (the correct location of the script), and save; then quit *Mathematica, restart it, and use Evaluation > Start Kernel and select that kernel, then I get a pop-up window "Kernel License Failure" saying the kernel "was not able to start due to licensing terms...." $\endgroup$
    – murray
    Sep 28, 2017 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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