Reputation: 561
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
.
Upvotes: 0
Views: 1184
Reputation: 9062
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
Upvotes: 2