Getting started with Mathematica packages

Can someone offer some tips on how to get started with math packages?

I can save the following in a file called "Foo.m". When I run the input cell in this file, I see the effects reflected in the $ ContextPath variable.

BeginPackage["Foo`"]

bar::usage = "barfunction";

Begin["`private`"]
bar[w_] := w;
End[];

EndPackage[];

However, on my laptop I have the following:

#include<foo>
SetDirectory[ToFileName[{$HomeDirectory, "My Documents", "mathematica"}]];
Needs["Foo`"]
$ContextPath

But for some reason, the call for some reason does not work, and $ ContextPath remains unchanged.

Edit

I believe that now I have a partial solution, the cell in my file has not been marked as an initialization cell - and so far I can now <<Foo', Needs["Foo"]` still does not work correctly.

+3
source share
2 answers

, "Foo.m" $Path, Mathematica, , PATH Unix Windows,

: $ContextPath , BeginPackage ( , Set Block - ).

: , ,

FileNames["Foo.m", $Path]

. , , .

+2

. Get ( <<) Needs, DirectoryStack[], $Path. (SetDirectory $Path, FileNames["Foo.m", $Path] Foo.m.) FindFile Directory[] $Path. , :

FindFile["Foo`"]
SetDirectory[<Foo dir>]
FindFile["Foo`"]

$Failed
<Foo dir>
<Foo dir>/foo.m

FindFile Foo.m, Needs . , $UserBaseDirectory/Applications, Needs .

+1

Source: https://habr.com/ru/post/1722746/


All Articles