artiq
artiq copied to clipboard
Import: Inconsistencies, when relatively importing modules inside an experiment's method
Bug Report
One-Line Summary
Importing modules (e.g. sub-experiments) relative to an experiment works in global context but not in methods of an experiment.
Issue Details
Lets assume two experiment files: a.py
and cfg/a_cfg.py
.
# a.py
from artiq.experiment import *
from artiq.language import *
class A(EnvExperiment):
def build(self):
from cfg.a_cfg import config
def run(self):
pass
# cfg/a_cfg.py
class config:
some_var = 1
Running the experiment a.py
via artiq_run
or the dashboard fails with
ModuleNotFoundError: No module named 'cfg'
Importing the cfg
module in the global scope of a.py
however now allows importing submodules inside methods (I consider this a workaround but not a fix). Importing submodules in the global scope also work as expected.
The issue comes from master.artiq.tools when calling file_import
: Here, the experiments path is first added to sys.path
for importing the module and then removed from sys.path
after the import. This explains, why importing in global scope works.
Once the experiment is instantiated, sys.path
does not contain the experiment's path anymore. So the build()
method will fail on import on my example. The same is true for any other method, that now is getting called after the experiment has been instantiated and the experiments path was removed from sys.path
.
Steps to Reproduce
-
artiq_run a.py
- Observe error message
Expected Behavior
Importing modules inside methods of an experiment relative to the experiments path should work similar as in the global scope of an experiment.
Actual (undesired) Behavior
Importing modules inside a method of an experiment relative to the experiment's path fails.
Your System (omit irrelevant parts)
- Operating System: Arch Linux
- ARTIQ version: ARTIQ v8.8396+115415d.beta
- Version of the gateware and runtime loaded in the core device: 8.0+unknown.beta; (compiled against https://github.com/m-labs/artiq/commit/115415d1204a2c69f41aa4c35e187f8a30855b1d)
- Hardware involved: Artiq Kasli