1
$\begingroup$

I have a Notebook cell with multiple line of codes. Since their only purpose is to make configurations and introduce self-defined functions, I would prefer to separate them from my main code. Therefore, it would be comfortable to save the code inside another Mathematica file and then just call it from my main document in the beginning to make all settings. Like:

Main:

MyConf[];

Library:

MyConf[] := [
    Print["Hello World!"];
    Print["XYZ"];
]
$\endgroup$
  • 1
    $\begingroup$ You can make a binary file with definitions of needed variables using DumpSave["file",{variables}] and read it by Get["file"]. The saved values will be assigned to the variables when you run the Get[..] $\endgroup$ – Rom38 May 31 at 7:35
  • 1
    $\begingroup$ That's exactly what packages do. $\endgroup$ – Roman May 31 at 17:50
1
$\begingroup$

Write these definitions in a file with the extension .m or .wl. You can create such a file with the menu item New -> Package/Script -> Wolfram Language Package.

Then you can load that file (i.e. evaluate its contents) using Get["/path/to/file.wl"]. See Get.


This is the first step towards creating a package. If you want to place this code in a separate namespace, and make it behave as packages do, take a look here: https://mathematica.stackexchange.com/a/132960/12

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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