21
$\begingroup$

I've quite forgotten: what file does one modify in order to add directories to $Path globally? Specifically, I want to include $UserBaseDirectory/ExtraPackges.

Or does one do it by means of modifying some setting in the OptionInspector (for Global Preferences) and, if so, which setting? I didn't see a relevant one under Global Options>File Locations.

$\endgroup$
1
  • 1
    $\begingroup$ Szabolcs suggestion is not working for me. Anytime I quit Mathematica everything i added gets removed. I've tried running as an administrator and that doesn't help. $\endgroup$
    – Ben
    Commented Oct 19, 2016 at 5:04

2 Answers 2

28
$\begingroup$

You can add the following line:

AppendTo[$Path, FileNameJoin[{$UserBaseDirectory, "ExtraPackges"}]]

to the file:

FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}]

init.m is described here, under "more information".


To do this automatically you can run:

With[
 {newPath = FileNameJoin[{$UserBaseDirectory, "ExtraPackges"}]},
 PutAppend[
  Unevaluated @ AppendTo[$Path, newPath],
  FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}]
 ]
]
$\endgroup$
4
  • $\begingroup$ I suspected the method was to include such an AppendTo, but the issue was which init.m to use. Just to be sure: one uses init.m for Kernel, not for FrontEnd? And there's no way to do this via OptionInspector? $\endgroup$
    – murray
    Commented Feb 16, 2012 at 17:16
  • 2
    $\begingroup$ I don't know if there is a way to do it from the Options Inspector. I tried the init.m in the indicated (Kernel) folder and it worked; my mental model of what is going on is: kernel gets started, loads init.m and runs it, then starts normal execution. So I just appended the path in there. Basically I think of it as autoexec.bat projected roughly 25 years into the future. $\endgroup$
    – acl
    Commented Feb 16, 2012 at 17:23
  • 1
    $\begingroup$ @murray $Path is a kernel variable, so this needs to be done for Kernel/init.m, exactly as acl said. I don't think the front end could execute AppendTo at all.. $\endgroup$
    – Szabolcs
    Commented Feb 16, 2012 at 17:40
  • $\begingroup$ OK, @Szabolics. So the obvious next question is: how could one learn that $Path is a kernel variable, unknown to the front end directly? $\endgroup$
    – murray
    Commented Feb 16, 2012 at 23:54
2
$\begingroup$

You could modify the global options in the options editor (Win.OS**Ctr + Shif + O**).

look up the EvaluatorStartup and put code

AppendTo[$Path, FileNameJoin[{$UserBaseDirectory, "ExtraPackges"}]]

into the value box and restart the frond end, check the $Path again. Note that ; is necessary to separate different codes.

enter image description here

Update: for multi-paths, use this code

If[!MemberQ[$Path, #] && DirectoryQ@#, 
   AppendTo[$Path, #]] & /@ {FileNameJoin[{$UserBaseDirectory, "ExtraPackges"}],"C:\\temp\\"}

if user defined package MyPackage` located in C:\temp\, then you can find the init.m by

FindFile["MyPackage`"]

and load it with this code

Get[FindFile["MyPackage`"](*,CharacterEncoding -> "UTF8"*)(*optional*)]
$\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.