简体   繁体   中英

Importing local modules into Snakemake files

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM