Import in a DAT, but acting like import in textport

if I import mymodule in the textport, i can use mymodule.foo in the parameters of any OP.

how can trigger this “global” import from a DAT?

I wouldn’t rely on that behavior, in fact the namespace of textport has been separated from parameter namespaces in later builds.

Instead consider using import(“name”) function in parameters.

Cheers.

Hi Rob

thank you. Unfortunately I didn’t have any success with import(“some_package”) inside a parameter. It always errors with invalid syntax. same in textport.

And if it works, how would I, in the same parameter access an attribute of the package?

Thanks again!

Are you using the double underscores? (Sorry, they got formatted earlier).
(No spaces as well)

__ import __(name)

If so, this returns the module object, and you use it the same as if you had just imported that word.

ie:

import X
X.y.func()

becomes

__ import __ (“X”).y.func() (no spaces)

1 Like

perfect. that works. thank you ! Hopefully the final question.

With the following code in some_package/__init__.py

# handle relative (tox) paths
Folder = __path__[0]
Tox = f"{Folder}/net.tox"
Icon= f"{Folder}/icon.png"

# handle extension
from some_package import ext 
Ext = ext.Class

I now can do:

  • par.externaltox: __import__("pysubprocess_op").Tox

Is there a way to modify this so i can skip the __import__ for other parameters (in the same op)?

So that I can just type:

  • par.extension1: some_package.Ext(me) or even better just
  • par.Icon: Icon

If I am not mistaken the externaltox parameter is evaluated before all other params (during file load) . Therefore the package is always imported and ready to be referenced from those parameters

We can’t guarantee the order the parameters are evaluated in, and if they share a module namespace that may change, so each expression really should include the __import__ call.
However the import call itself, used in this way should be very lightweight if that helps at all.

Cheers,
Rob.