16
$\begingroup$

I am trying to run bash commands from within Mathematica. Funnily enough, Mathematica seems to use a different $PATH environment than the system environment. If I try

Environment["PATH"]

or equivalently in Mathematica 10

RunProcess[$SystemShell, All, "echo $PATH"]

I get (in StandardOutput):

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

However, if I run echo $PATH on my (OS X) terminal, I get (both as user and root):

/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin/:/usr/texbin:/Users/frederik/bin

which is indeed the full path. Why doesn't use Mathematica the same environmental variable? Is this expected behaviour?

EDIT

I already mentioned in the comments why this question is different from this one, but to avoid getting this question marked as a duplicate I'll repeat it here.

That thread does not deal with system environment variables (it's an unfortunate wording in the topic title) but with Mathematica environment variables. The path variable for the latter is stored in $Path in Mathematica, while the former can be accessed by executing Environment["PATH"] (which gives the OS system environment path). I'm really asking about the system environment variable here, but thanks to ilian and m_goldberg I realise now that it is an OS X issue.

$\endgroup$
5
  • $\begingroup$ @dionys I've seen that one as well, but that thread does not deal with system environment variables (typo in the topic) but Mma environment variables. See the difference between $Path and 'PATH'in Mma. I'm really asking about the system environment variable here, which you access with Environment["PATH"]. $\endgroup$ Commented Nov 17, 2015 at 16:33
  • $\begingroup$ @dionys When I run a bash command inside Mma using Run[] or RunProcess[], it will only recognise commands inside the system wide $PATH variable (Unix notation). What I don't understand is why it doesn't use my system path, but seems to use some default unix path which is only part of it. $\endgroup$ Commented Nov 17, 2015 at 16:38
  • $\begingroup$ @dionys No problem, thanks for your comment :-). $\endgroup$ Commented Nov 17, 2015 at 16:38
  • $\begingroup$ The docs for Environment indicate the path is system dependent ... perhaps your shell sets a user-specific environment that differs from the system default? RunProcess also has a ProcessEnvironment option you can take advantage of. $\endgroup$
    – dionys
    Commented Nov 17, 2015 at 16:44
  • $\begingroup$ I'm having this problem in Windows mathematica $\endgroup$
    – Rainb
    Commented Jul 8, 2020 at 7:18

3 Answers 3

9
$\begingroup$

It is certainly an OS X issue. Assuming the default shell is configured, when Terminal starts up it runs the script file ~/.bash_profile, which usually modifies the PATH environment variable. You can do the same thing in Mathematica with

SetEnvironment["PATH" -> Import["!source ~/.bash_profile; echo $PATH", "Text"]]

After which

Environment["PATH"]

should match what you see in Terminal.

$\endgroup$
5
  • $\begingroup$ It still doesn't match up fully. After sourcing .bash_profile, most paths in /opt, /usr, /bin and /sbin are present, but the paths in /opt/X11, /Library/TeX and /usr/texbin are still missing. So probably OS X has another source as well for its $PATH variable, but I guess it's no longer a question for Mathematica.SE anymore then.. $\endgroup$ Commented Nov 17, 2015 at 22:39
  • $\begingroup$ @freddieknets the other OS X source for $PATH is /etc/path and the files in /etc/paths.d. They are loaded by the OS X utility path_helper. $\endgroup$
    – Virgil
    Commented Jan 27, 2016 at 16:59
  • $\begingroup$ This worked... but then didn't. Nonetheless up vote :) $\endgroup$
    – SumNeuron
    Commented Jun 23, 2017 at 19:12
  • $\begingroup$ Mathematica 11.2 under macOS Sierra (10.12.6) is not getting its value of Environment["PATH"] from /etc/path and the files in /etc/paths.d. In fact, the output from Environment["PATH"] is only "/usr/bin:/bin:/usr/sbin:/sbin"; however, /etc/path includes the additional path /usr/local/bin, and on my system /etc/paths.d includes among others the file TeX containing the additional path /Library/TeX/texbin. $\endgroup$
    – murray
    Commented Sep 24, 2017 at 14:46
  • 1
    $\begingroup$ I know this question specifically mentions OS X and that it was thus pointed out that it was an OS issue. I just want to add a comment for someone who encounters this in Ubuntu that I am having the same Issue on Ubuntu 20.04 so OS X is not alone in this regard. $\endgroup$
    – Kvothe
    Commented Jan 18, 2021 at 16:27
12
$\begingroup$

I think this is an OS X issue. Note that if you launch Mathematica from a terminal, you will get the value of Environment["PATH"] which you expected.

For GUI applications that are started through Finder/LaunchServices, the environment (and $PATH in particular) is different and determined by the OS (this is a related recent question that had to do with setting up umask).

This behavior may be changed, though the exact mechanism seems to evolve with each OS X release and I don't know off the top of my head what is the best approach to achieve this on Yosemite or El Capitan.

$\endgroup$
1
  • 2
    $\begingroup$ Indeed, launching Mathematica from Terminal was the most expeditious solution for me, and it will always work. In a Terminal window, type /Applications/Mathematica.app/Contents/MacOS/Mathematica &. (Leave that window open!) Then in Mathematica, you can verify that it worked with Environment["PATH"]. $\endgroup$
    – Mark Adler
    Commented Dec 29, 2022 at 18:40
5
$\begingroup$

As noted by @ilian the easiest solution is to simply launch Mathematica from a Terminal window that has a PATH you like. On macOS:

/Applications/Mathematica.app/Contents/MacOS/Mathematica &

An even easier way is to launch Mathematica by opening a notebook using the open command from a Terminal window:

open whatever.nb

Then that instance of Mathematica will inherit the PATH of that Terminal session.

Or you can set the path directly in any notebook in which you care about this, as noted by @m_goldberg. I like this solution the best, since it is the least obtrusive and you can add only what you really need, e.g.:

SetEnvironment["PATH" -> "/usr/local/bin:" ~~ Environment["PATH"]~~":/Users/myname/bin"]

If you would like to set an application path permanently on macOS, you can:

sudo launchctl config user path $PATH

and then reboot and restart all applications (lest they hold the old path through the reboot). That will set the path for all applications and all users to the path you have in the terminal you ran that command from.

You could instead echo $PATH and then copy just the parts you want into the the launchctl command.

This can be undone on macOS with:

sudo defaults delete /private/var/db/com.apple.xpc.launchd/config/user.plist PathEnvironmentVariable

For any solution, you can verify the path the Mathematica kernel is currently using with:

Environment["PATH"]
$\endgroup$

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.