0

How do you import local files (.py files) into Snakemake files (.smk files)? I have the following example structure:

parent_dir
├── dir1
│   └── a.py
└── dir2
    └── b.smk

I would like to import a.py into b.smk. I tried the following:

sys.path.insert(0, Path(__file__).parent.parent)
import dir1.a

but had no success. It results in a ModuleNotFoundError. Is there a way around this? I do not want to change the extension of a.py to .smk.

yippingAppa
  • 331
  • 1
  • 11
  • 1
    Importing a module into a Snakefile shouldn't be all too different from importing a module into a regular python file - do any of the answers to this help? https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – KeyboardCat Jul 13 '22 at 06:21
  • It's not the same though because the reason why there is a `ModuleNotFoundError` is because when you run snakemake, the path of `__file__` is not the path to `b.smk`. It is the path to `workflow.py` which is an internal Snakemake file. Try it for yourself – yippingAppa Jul 13 '22 at 17:28

1 Answers1

2

As you noticed, __file__ gives the path to the snakemake module workflow.py. You can access the path to the snakefile with workflow.basedir (I think there is a better method though, check the documentation). Also, I think you need to convert the Path object to string. So something like:

sys.path.insert(0, Path(workflow.basedir).parent.as_posix())
print(sys.path) # for debugging

import dir1.a

rule all:
    input:
        ...

then inside directory dir2 execute: snakemake -s b.smk -j 1 ...


Edit: I think workflow.basedir is a bit of a hack; in fact, it is not even documented. Better would be to look at accessing auxiliary files

dariober
  • 8,240
  • 3
  • 30
  • 47