I need to get the .m "package" file with the code from my notebook to use it later in a cluster (without graphical user interface) and I don't know how to get the file.

  • 1
    $\begingroup$ It is not quite clear what you are asking. Please edit the question and explain what you want to do through a concrete example. I do not understand what it means "to get the .m file". $\endgroup$
    – Szabolcs
    Commented Feb 16, 2017 at 17:10
  • $\begingroup$ Possible duplicate: mathematica.stackexchange.com/questions/29324/… $\endgroup$
    – Michael E2
    Commented Feb 9, 2021 at 15:47

1 Answer 1


Manual solution

Mark exported cells as initialization cells and use Save as in the File menu. Choose 'Wolfram Mathematica Package' as output. See here for more information about packages.

This saves all of your Wolfram language code in the notebook as "wolfram package" file in plain text code in a file with extension .m. You will see all code not marked to be initialization cell still in the .m file but it will be commented out. Only the cells marked as initialization cells will not. Thus, you need to mark specifically the code you want to run on the cluster to an initialization cell. The nice thing about it is that you can have in corresponding "debugging notebook" (.nb file) your debugging and development code, extra text cells and graphics which the "cluster" does not care about.

Automatic solution

If you keep working like this a lot, you might get bored of saving .m files from your .nb files by hand. What you can do, is to navigate Format->Options inspector->Notebook Options->File options, change Show option values to Selected Noebook and set AutoGeneratedPackage to Automatic. Then you will get a .m file each time you save the .nb file with all of the initialization cells as a script.


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