# Import a .wl file into .nb format to help understand and study it

**URL:** <https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677>\
**Category:** Questions\
**Tags:** software-development, import-and-export, packages-and-paclets\
**Created:** [February 25, 2022, 2:38pm UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677 "2022-02-25T14:38:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 25, 2022, 2:38pm UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/1 "2022-02-25T14:38:25Z")

</div>

In Mathematica, a package is often stored in .wl file format, which has a very different writing style and execute behavior from those of the notebook (.nb) file format.

IMO, the .nb file is more straightforward to learn, study, and develop. So, I want to know if I can import a .wl file into .nb format to help understand and study it.

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![hansmilton](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hansmilton/32/8656_2.png) [@hansmilton](https://community.wolfram.com/u/hansmilton)\
**Post date:** [February 25, 2022, 8:28pm UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/2 "2022-02-25T20:28:20Z")

</div>

No problem. Just open the package .wl file and save it as a .nb

[1. SimplePackage.wl](https://community.wolfram.com/uploads/short-url/b6HXtgdCfI9fySDu9hm14Ps6yOC.wl) (285 Bytes)  
[2. SimplePackage.nb](https://community.wolfram.com/uploads/short-url/4VCWwGLP4Rdaaa7gsTDzGQEpPd0.nb) (2.83 KB)

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 26, 2022, 3:04am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/3 "2022-02-26T03:04:08Z")

</div>

Thank you for your minimal example. But for a truly complex project, should I develop in nb format first, then convert to wl?

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![hansmilton](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hansmilton/32/8656_2.png) [@hansmilton](https://community.wolfram.com/u/hansmilton)\
**Post date:** [February 26, 2022, 6:52am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/4 "2022-02-26T06:52:43Z")

</div>

I do the development, function by function, in notebooks. Once a function is debugged I copy it to the package wl file.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 26, 2022, 8:08am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/5 "2022-02-26T08:08:32Z")

</div>

Thank you for sharing your practice.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 26, 2022, 1:15pm UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/6 "2022-02-26T13:15:08Z")

</div>

Another problem is: Take the following code snippet as an example, as shown in notebook:

```
{{1, 2}, {3, 4}} // MatrixForm

```

If I exported it into wl format, I want the beautification related command, e.g., **MatrixForm** used here, is automatically removed, but I still observed the following exported result via **Save As** :

```
$ cat Untitled-1.wl 
(* ::Package:: *)

(* ::Input:: *)
(*{{1,2},{3,4}}//MatrixForm*)

```

I’m not sure if there are any tricks to solve this problem.

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [February 27, 2022, 12:20am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/7 "2022-02-27T00:20:57Z")

</div>

Hongyi,

Only `Initialization` cells are saved to the `wl` file as uncommented code. Add the `Initialization Cell` property to the cells you want to export as code. From the menu Cell / Cell Properties / Initialization Cell. On a Mac the shortcut is Control 8.

You could also add the following to the notebook

```
SetOptions[EvaluationNotebook[], AutoGeneratedPackage -> Automatic]

```

After evaluating it, whenever the notebook is saved a corresponding `.m` file is created for all `Initialization` cells. I often put expressions that are used in many different projects into such a notebook and use `Get` to use the expressions in the `.m` file in another notebook.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 27, 2022, 1:06am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/8 "2022-02-27T01:06:34Z")

</div>

Rohit Namjoshi,

Thank you for your comments. But according to the explanation [here](https://wolfram.discourse.group/t/13172/1) :

```
The .m suffix is the old way of saving package files, and is used only for backwards compatibility. Use .wl for new stuff.

```

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [February 27, 2022, 2:44am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/9 "2022-02-27T02:44:50Z")

</div>

Hongyi,

For backwards compatibility `AutoGeneratedPackage -> Automatic` still uses the old `.m` extension even though `.wl` is recommended. Matlab also uses the `.m` extension which is probably why its use for Wolfram Language files is discouraged.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 27, 2022, 6:52am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/10 "2022-02-27T06:52:16Z")

</div>

I checked with the following notebook, and the corresponding .m file will be updated each time I save the notebook.

But I hope that the following line appears in the .m file:

```
PauliMatrix/@{1,2,3}

```

Instead of the following one:

```
PauliMatrix/@{1,2,3}//MatrixForm

```

Regards,  
HZ

[SimplePackage.m](https://community.wolfram.com/uploads/short-url/sJtrZkeGsEC2JAs5t5IrPfI4nuE.m) (1.39 KB)  
[SimplePackage.nb](https://community.wolfram.com/uploads/short-url/xkMjlh02fTgjJqpjYo2V48Ofh1d.nb) (7.91 KB)

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [February 27, 2022, 7:49pm UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/11 "2022-02-27T19:49:45Z")

</div>

I don’t understand the question. If you want this in the output `.m` file

```
PauliMatrix/@{1,2,3}

```

then you have to have the same expression in the input `.nb` file. You have this in the input `.nb` file

```
PauliMatrix /@ {1, 2, 3} // MatrixForm

```

so that is what appears in the output `.m` file.

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 28, 2022, 12:47am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/12 "2022-02-28T00:47:10Z")

</div>

I mean: Stripping the **// MatrixForm** when saved as .m file automatically.

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [February 28, 2022, 2:04am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/13 "2022-02-28T02:04:44Z")

</div>

How do you expect the system to know what expressions you want to be modified and exactly how they should be modified before they are saved?

What if you have

```
mf[m_] := Row[{"The matrix is:", MatrixForm[m]}]
PauliMatrix /@ {1, 2, 3} // mf

```

Do you expect `// mf` to be removed?

Your question appears to be an [XY Problem](https://xyproblem.info/).

---

<div class="post-metadata">

**Author:** ![hongyizhao](https://avatars.discourse-cdn.com/v4/letter/h/8491ac/32.png) [@hongyizhao](https://community.wolfram.com/u/hongyizhao)\
**Post date:** [February 28, 2022, 5:50am UTC](https://community.wolfram.com/t/import-a-wl-file-into-nb-format-to-help-understand-and-study-it/19677/14 "2022-02-28T05:50:11Z")

</div>

It’s not exactly an XY Problem. My core idea is how to automatically remove code fragments that beautify and increase readability when saving as a .wl file, thus increasing the compactness and simplicity of the source code.
