在Snakemake中引用GitHub模块时遇Python模块找不到问题求解决
问题描述
我想在新的Snakemake工作流中,将GitHub上的distiller工作流作为模块使用,模块定义代码如下:
module distiller: snakefile: github("open2c/distiller-sm", path="workflow/Snakefile", branch="master") # "distiller-sm-master/workflow/Snakefile" config: config use rule * from distiller as distiller_*
该distiller模块会从GitHub仓库的本地.py文件导入函数,代码如下:
from .scripts.common import organize_fastqs, needs_downloading
运行新工作流时出现报错:
ModuleNotFoundError in file https://raw.githubusercontent.com/open2c/distiller-sm/master/workflow/Snakefile, line 4: No module named 'snakemake.scripts'
请问有什么办法可以解决这个问题?
解决方法
手动克隆仓库并使用本地路径
直接把open2c/distiller-sm仓库克隆到本地,修改模块定义里的snakefile路径为本地绝对路径:module distiller: snakefile: "/path/to/local/distiller-sm/workflow/Snakefile" config: config use rule * from distiller as distiller_*这种方式能让Snakemake基于本地仓库的目录结构正确解析相对导入,彻底规避远程加载带来的路径问题。
调整导入逻辑
如果必须用远程模块,可以在主工作流的Snakefile开头添加代码,把distiller的scripts目录加入Python路径:import sys from pathlib import Path # 替换成实际的distiller scripts目录路径,可通过克隆仓库或Snakemake缓存获取 distiller_scripts = Path("/path/to/distiller-sm/scripts") sys.path.append(str(distiller_scripts))同时修改distiller模块里的导入语句,把相对导入改成绝对导入:
from scripts.common import organize_fastqs, needs_downloading注意:这种方式需要修改distiller的代码,若模块是远程加载的,可能需要先克隆仓库修改后再使用。
指定模块工作目录
在模块定义中添加workdir参数,指向本地distiller仓库的根目录,让模块在正确的上下文执行:module distiller: snakefile: github("open2c/distiller-sm", path="workflow/Snakefile", branch="master") config: config workdir: "/path/to/local/distiller-sm" use rule * from distiller as distiller_*这样模块内部的相对导入会基于指定的
workdir解析,避免路径错误。
内容的提问来源于stack exchange,提问作者Phlya
相关产品推荐
相关产品推荐

