Importing libraries

I have a task that requires itertools. but I am having no luck. is it possible to import from itertools? or any other library for that matter? thanks

You cannot import libraries in Scripting app.

You can copy/paste in minified versions of libraries into your script if they are small enough and in a single file.

There are also creative ways of having the script itself get raw library files and use eval.

Or you can write a custom app, which can import node modules.

3 Likes

like this one

Many thanks to @Bill.French and @Jeremy_Oglesby !

oLĎ€

1 Like

I personally don’t like storing libraries in tables where users who don’t know any better can do unexpected things with them.

An alternative is to get the library from a cdn. This method is slightly better because base users can’t mess with the library. However, keep in mind that every time you use eval you have to really trust the source.

This example gets the mathjs library from a cdn. It isn’t very useful since you can use use Math out of the box, but in theory it should work with other libraries.

Plus, it looks like you can’t use eval in automation scripts.

await getMathJs()
async function getMathJs() {
  const response = await fetch("https://cdnjs.cloudflare.com/ajax/libs/mathjs/7.5.1/math.min.js");
  const minifiedLibrary= await response.text();
  eval(minifiedLibrary)
}

let answer = math.round(math.e, 3)
output.inspect(answer)

answer = math.round(math.pi, 15)
output.inspect(answer)
1 Like

I have used eval() in the past without issue.

I doubt this would work in any case because eval() would likely trip over anything minified. Try …

eval('0 == 1')

Pretty sure it should work.

eval won’t work for me in an automation action script. I get an error saying that it is not defined.

image

Why wouldn’t eval() work on something minified? Minified code is still valid code. The code that I posted works for me in scripting app.

Good to know - so this is simply an issue with script actions I guess. That’s unfortunate - there should be no limitation on eval; importing libraries is a different animal.

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.