Jump to content

Relative module imports within Add-On Fails


---
 Share

Recommended Posts

---

Hi, we're trying to run tensile kiosk mode in 2023 Correlate.  The GOM-created script uses several relative imports.  For example in the StandardTensileKiosk_nonmetallic, it has 

from .. import Functions

This is failing with an error saying that the module is attempting to import beyond the top level package.  But it isn't.  The import is clearly referenced to a relative path within the top-level package.  When we made our own separate Add-On to experiment with relative import issues, we also observed an error saying that the module parent was not defined.

This issue has rendered tensile kiosk mode inoperable in 2023, and it is essential for our operations.  We are willing to make custom edits to the tensile kiosk mode and deploy it throughout our organization, but we're unclear on how to patch the code to make relative imports work within an Add-On.  Thanks for any help!

Link to comment
Share on other sites

---

Hi,

does it work to import a package using an absolute path?

E.g.:

image.png.bfdb3dbdfccd7a7a366d0fd56ccea81f.png

# -*- coding: utf-8 -*-
# submodule.py
import gom
from Toplevel import topmodule

Regards,

Matthias

Link to comment
Share on other sites

---

Please sign in to view this quote.

Thanks for the fast reply!  No, it didn't.  In the case of the tensile kiosk, the top-level is called "Kiosk", but when we try

from Kiosk import Functions

we similarly get an error saying Kiosk is not a known package.  (I apologize, I don't have access to machine at the moment to get a better image).  So if `local_dependency.py` is a file inside Kiosk along with a subfolder `sub` and a submodule `sub/subfile.py`, then the contents of Kiosk are on the path, but not the Kiosk package itself....so absolute import `Kiosk.local_dependency` does not work and neither does relative import `..local_dependency`.

Link to comment
Share on other sites

---

How do you start the Kiosk ? Where is the starting script located ?

(Sorry, it's been very long since I looked at aramis kiosk, and I don't have anything setup to try...)

 

Link to comment
Share on other sites

---

The startup script is called StartKiosk.py and is in a folder called "Kiosk" which is in the tensile kiosk Add On.  It then loads classes from a subfolder called StandardTests.  The files inside StandardTests try to load modules called Functions.py, Dialogs.py, etc. from the same folder as StartKiosk using relative import.
 

Link to comment
Share on other sites

  • 3 weeks later...
---
Posted (edited)

I have got it running, well at least it runs until the "Start Menu"-dialog with the "Prepare Measurement"-button, so it looks like all imports are working here.

Sorry, I cannot test this further, no equipment ready...

What have I done:

1) Move start scripts out of the "Kiosk" package:

Move_start_scripts.png.82a7d1d498b772a08d274f60947efca1.png

and adapted the imports to use relative imports into the "Kiosk" package. There are two places, the one shown in the picture
and in lines 113ff:

    def tryToImportStandardTest(name=""):
        try:
            exec('from Kiosk.StandardTests import %s' % name)

2) Adapt the imports in the "Kiosk" package to use relative import from ".", for example in Basetest.py:


# import from standard system scripts:
from . import Functions
from . import Dialogs
from . import Variables

There are about 10 imports in the toplevel scripts in "Kiosk".

 

I hope this helps and that it's also working for the complete workflow.

 

Edited
Link to comment
Share on other sites

 Share

×
×
  • Create New...