glamorous-pillow-95848
12/16/2024, 10:01 PMfrom metaflow import Run, Runner
def test_sample():
metaflow_data = run_pipeline()
assert metaflow_data.mlflow_run_id is not None
def run_pipeline():
with Runner("pipelines/training.py", environment="conda").run() as running:
return Run(running.run.pathspec).data
This works for some of the flows, but it fails for the training.py flow in this example because that file imports some functions from a separate common.py file.
Here is the directory structure:
├── pipelines/
│ ├── training.py
│ └── common.py
└── tests/
└── test_sample.py
Here is the error I get when I try to run the test (and the Runner tries to execute the training flow):
> from common import packages
E ModuleNotFoundError: No module named 'common'
I tried fixing this problem by adding a symlink inside /tests pointing to /pipelines. I also tried to fix it by passing cwd to Runner pointing to the /pipelines folder.
Any suggestions?crooked-jordan-29960
12/16/2024, 10:08 PMRunner(..., cwd="../pipelines"). @brainy-truck-72938 could you confirm?glamorous-pillow-95848
12/16/2024, 10:09 PMcwd argument is there, but I can’t get it to work. I’ve tried this:
1. Setting cwd to the relative location of `/pipelines`: cwd="../pipelines"
2. Setting it to the absolute path: cwd="/Users/…/pipelines/"
It doesn’t work in either case.crooked-jordan-29960
12/16/2024, 10:12 PMtests/common.py to point at the actual pipelines/common.py file.
On the SDK-based path, I'll wait for Madhur or someone with more knowledge of that feature to answer.glamorous-pillow-95848
12/16/2024, 10:14 PMsubprocess as specified here: https://docs.outerbounds.com/use-pytest/)brainy-truck-72938
12/17/2024, 7:18 PMcwd argument only affects where the subprocess execution will take place, it doesn't affect where the flow file is loaded from. Thus, the cwd option doesn't work, because at the time of loading the file, the from common import packages needs to work..
I was able to make it work by using PYTHONPATH i.e.
PYTHONPATH=/Users/madhur/Desktop/outerbounds/office-hours/sant/pipelines python tests/test_sample.py
here, the folder sant has this structure:
. <-- this is `sant`
├── pipelines
│ ├── common.py
│ └── training.py
└── tests
└── test_sample.pybrainy-truck-72938
12/17/2024, 7:41 PMcwd at all then..brainy-truck-72938
12/17/2024, 7:41 PMglamorous-pillow-95848
12/17/2024, 7:44 PMPYTHONPATH workaround works… but it’s ugly 🙂 It looks like you submitted a patch to fix this without having to use PYTHONPATH or cwd. Is that what the patch does?brainy-truck-72938
12/17/2024, 7:45 PMflow_dir = os.path.dirname(os.path.abspath(flow_file))
sys.path.insert(0, flow_dir)
and then also removes it later in a finally block
sys.path.remove(flow_dir)glamorous-pillow-95848
12/17/2024, 7:46 PMPYTHONPATH (set via pytest.ini), but it breaks Metaflow’s colors and it feels like a hack.brainy-truck-72938
12/17/2024, 7:47 PMbrainy-truck-72938
12/17/2024, 7:50 PM_return_ running.run.data
running.run gives you the Run object so you don't need to construct it explicitlyglamorous-pillow-95848
12/17/2024, 7:52 PM