9
$\begingroup$

I am trying to import a package that works correctly when I use it in a notebook in a wolframscript .wls. I get a weird shadowing problem, however.

Here is a minimal working example test.wls, using the example from the Needs documentation:

#!/usr/bin/env wolframscript -print All

Needs["ComputerArithmetic`"]

Ulp[1000.]

after chmod +x test.wls the result of running it is

> ./test.wls

Ulp::shdw: Symbol Ulp appears in multiple contexts {ComputerArithmetic`, Global`}; 
definitions in context ComputerArithmetic` may shadow or be shadowed by other
definitions.
Global`Ulp[1000.]

However, when I copy and paste the two non-shebang lines of code from test.wls into a notebook, I get the correct result. The same thing happens with my custom package.

I tried runnning wolframscript -code '$Path' and the directory that contains my package is there, so it cannot be a screwed up path.

If I change the final line of my script to

ComputerArithmetic`Ulp[1000.]

I get as output 1.1368683772161603*^-13, and the same fix works for my own package.

Question: what is going on here, and what is a good workaround so that I don't have to specify the context of every custom package procedure and variable?

$\endgroup$
  • $\begingroup$ For completeness, I have $Version=="12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)" $\endgroup$ – evanb Nov 27 '19 at 1:14
  • $\begingroup$ I guess I should also say that I have wolframscript --version WolframScript 1.2.0 for MacOSX-x86-64. $\endgroup$ – evanb Nov 27 '19 at 4:45
  • $\begingroup$ It seems to be fine if you remove -print All and use e.g. Echo inside. $\endgroup$ – Kuba Nov 28 '19 at 8:48
  • $\begingroup$ You're right, that is a solution. But the documentation reference.wolfram.com/language/ref/program/wolframscript.html suggests I should be able to use -print All (or maybe all) to get everything printed. In my "actual" script, rather than my mwe, making this change would be very very annoying. $\endgroup$ – evanb Nov 29 '19 at 15:11
8
$\begingroup$

Here's a version of the script that works:

#!/usr/bin/env wolframscript -print All -run \'Needs[\"ComputerArithmetic`\"]\'

Ulp[1000.]

This is a limitation of -print All that we probably should either document or find a fix for, though the latter is rather challenging. When executing a script normally or using -print, wolframscript can essentially tell the kernel to just Get the script file and print or not the value returned by Get. But for -print All, wolframscript effectively has to import the script prior to executing it to get all the individual expressions. This means the all the symbols are created prior to the Needs evaluating, and the package symbols end up in the wrong context. Using the -run option to the kernel (without lots of shell escapes) tells the kernel to load the package prioir to doing anything, so the symbols are created in the right context.

Update

After thinking about this some more, it probably doesn't actually have to import the file first--it's probably just doing that for efficiency. It could read it in one expression at a time, evaluate the expression, and print the result. This would be slower, but produce more accurate results. I'll file a bug about this.

$\endgroup$
  • 3
    $\begingroup$ Why does it have to import the script prior to execution, rather than reading it line-by-line (or similar) and executing it on the fly? $\endgroup$ – evanb Dec 2 '19 at 20:23

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.