Update (reload) mathematica package after changes

落花浮王杯 提交于 2019-12-04 17:31:45

问题


I'm trying to find a shortcut to the following loop. When developing a mathematica's package one is implementing changes to the .m file's code and then want to test the changes in another notebook. This is an infinite loop...

So, we have a package package.m and a notebook test_package.nb where testing is done. Currently, when ever I change something in the .m file, I then have to:

Quit[]
Needs["package`"]

in the notebook for the changes to become available, so I can test them. It seems like without the Quit[] it doesn't work.

Is there a shorter way to iterate this development loop? In particular avoid the quitting?

Aftermath

If I could, I'd accept both ruebenko and Leonid's answers as both are helpful and solve my problem. However, as only one answer can be chosen, I picked ruebenko's due to is simplicity and easiness - by simply changing the Needs to Get I overcame my problem. Nevertheless, Leonid's package is for sure more sophisticated and probably at the end of the day yields better results, but it has to be installed, loaded etc.


回答1:


Use Get; Need only loads the package if is not loaded at all. Sometimes you need to be careful and use ClearAll on the variables in the package.




回答2:


As an alternative to using simple Get, as sugested by @ruebenko (which is a good advice), you can try using a package I wrote called PackageManipulations`, which does what you want. It is available here. There is a notebook with examples illustrating how it works, also availabe at the page I linked. The package can reload, clear or remove a given package (context). The function PackageReload specifically will do what you want.

It does use Get or Needs under the hood, but in addition it will track the so-called "escaping symbols" (symbols which are publicly exported but have no usage messages, and may be "leaking" the implementation details), and optionally can resolve the shadowing problems in favor of the package being re-loaded. Note that PackageReload will automatically call Unprotect and ClearAll on package's symbols, therefore addressing the issue mentioned by @ruebenko. You can find more details in the example notebook which comes with the package.

Note that, while I used it myself many times, I don't claim it is bug-free :)




回答3:


The easiest method is to use Clear or, better yet, ClearAll on the exposed symbols in your package at the very beginning of your package, as follows

BeginPackage["SomePackage`"];
ClearAll[ ASymbol, AnotherSymbol, ...];

This ensures that you have a clean slate whenever you load it. This is easier once the package is more stable, but I find if I don't do this when I'm initially developing a package, it doesn't get done.



来源:https://stackoverflow.com/questions/8443430/update-reload-mathematica-package-after-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!