17
$\begingroup$

I don't want to necessarily put my packages in Mathematica's $UserBaseDirectory, which is on my local hard drive. Can I somehow tell Mathematica to look for packages in other directories (like DropBox)?

Basically, I have two computers running Mathematica -- one at home, and one at work, and would like to be able to work on editing/running my packages on both computers.

$\endgroup$
4
  • 1
    $\begingroup$ Adding the path to your local dropbox folder (or subfolder where you have stored the packages) does not work? reference.wolfram.com/mathematica/ref/$Path.html $\endgroup$
    – Peltio
    Commented Oct 28, 2013 at 22:16
  • $\begingroup$ Ok, I tried it, and that works! but only for one time. $Path gets reset every time I open Mathematica, so I need something more permanent. $\endgroup$
    – QuantumDot
    Commented Oct 29, 2013 at 7:53
  • $\begingroup$ Ok, the answer to my second question is here: mathematica.stackexchange.com/questions/1874/… $\endgroup$
    – QuantumDot
    Commented Oct 29, 2013 at 7:58
  • 1
    $\begingroup$ @Peltio Please put your response as an answer so I can accept it. (Please add a few words about making changes to $Path permanent). $\endgroup$
    – QuantumDot
    Commented Oct 29, 2013 at 7:59

3 Answers 3

13
$\begingroup$

The easiest way is to modify $Path to include the directory you placed your packages into. Suppose your packages directory is in the Dropbox\Documents\myPackages folder inside your home directory; then you can add it to your path like this:

AppendTo[$Path, ToFileName[{$HomeDirectory, "Dropbox", "Documents", "myPackages"}]]

(EDIT: amended according to advice by Steve Luttrell) To make this change permanent, you can add this line to your init.m file. Each system (home and work PC) will have a custom line of this sort in its init.m file that will tell Mathematica where to look for your packages.

Please note that since you are appending the new directory at the end of the path, system directories will have the precedence over custom ones. Just in case you decide to have the same names for different packages. If you want your packages to have the precedence, you have to change the order your directory appears in your $Path either by prepending it or by placing it at a custom position.

And now, for something extreme...
If you really want to have Mathematica look elsewhere for all its files, you might want to fiddle with $UserBaseDirectory . I have no experience with that, but the documentation says that "the value of $UserBaseDirectory can be specified by setting the operating system environment variable MATHEMATICA_USERBASE when the Mathematica kernel is launched. It cannot be reset from inside the kernel". I guess that by copying the actual userbase directory to your new Dropbox location and then by specifying MATHEMATICA_USERBASE accordingly in your OS environmental variables, you can get Mathematica to work directly with files in the Dropbox folder. But this is something you can do only on machines where you are an administrator, and it looks kinda risky...

$\endgroup$
6
  • $\begingroup$ Something went wrong when trying to associate links with $namedvariables. I had to add the links in a separate manner. $\endgroup$
    – Peltio
    Commented Oct 29, 2013 at 13:58
  • $\begingroup$ I fixed them for you. They didn't work because the markdown link syntax was incorrect. Links are entered as [link text](url) and url must have http(s):// $\endgroup$
    – rm -rf
    Commented Oct 29, 2013 at 16:00
  • $\begingroup$ Thanks. Every day I learn something new. :-) $\endgroup$
    – Peltio
    Commented Oct 29, 2013 at 16:04
  • $\begingroup$ Because you use AppendTo rather than Append you don't need $Path =. $\endgroup$ Commented Oct 29, 2013 at 17:54
  • $\begingroup$ Right, I edited the post to reflect this. $\endgroup$
    – Peltio
    Commented Oct 29, 2013 at 18:15
2
$\begingroup$

Another solution would be to create a symbolic link if you are working on Linux or Mac OS X. I don't know how to do this in windows. In any case, for this example I put the MATLink package in my dropbox folder. The location is the following:

/Users/jmlopez/dropbox/MATLink

To be able to use I need to put a shortcut to it

macbook-pro:~ jmlopez$ ln -s /Users/jmlopez/dropbox/MATLink /Users/jmlopez/Library/Mathematica/Applications/MATLink

Now I have two ways of accessing MATLink, One from the Dropbox folder and one from the hidden directory $UserBaseDirectory. Notice the dropbox icon.

Finder Image

I only provided this answer since I did not see anyone commenting on it. My preference would also be to modify the Kernel/init.m file in the $UserBaseDirectory as I would modify my bashrc file.

$\endgroup$
1
$\begingroup$

I assume this would also serve the purpose?

Get[FindFile["Dropbox\\yourpackage.m"]]
$\endgroup$
1
  • $\begingroup$ I'm afraid you may have misunderstood FindFile. FindFile searched for a file exactly the same way as Get, but instead of loading the file, it just returns the complete path. This is useful e.g. when you need to find out which of two identically named files would be loaded by Get/Needs, or if you just need the full path for any reason. So Get[FindFile[something]] will always be equivalent to Get[something]. If the latter doesn't work, neither does the former. $\endgroup$
    – Szabolcs
    Commented Jan 20, 2014 at 18:15

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.