Search code examples
pythonslurmsnakemake

NameError: name 'snakemake' is not defined when running snakemake


I have a sample script defined as:

#!/usr/bin/env python

def test(path):
    print(path)

test(snakemake.input[0])

A config.yml like:

executor: slurm
jobs: 100
samples: "config/samples.csv"

And a Snakefile like:

import os
import glob
import pandas as pd

configfile: "config/config.yml"

rule get_samples:
    resources:
        mem_mb = 512
    threads: 1
    input: config["samples"]
    output: "out/sampleFASTQs.csv"
    shell: "scripts/getFASTQs.py"

When I run

snakemake --cores 1

I get the following error:

Building DAG of jobs...
Using shell: /usr/bin/bash
Provided cores: 1 (use --cores to define parallelism)
Rules claiming more threads will be scaled down.
Job stats:
job            count    min threads    max threads
-----------  -------  -------------  -------------
get_samples        1              1              1
total              1              1              1

Select jobs to execute...

[Wed May 15 05:36:05 2024]
rule get_samples:
    input: config/samples.csv
    output: out/sampleFASTQs.csv
    jobid: 0
    reason: Missing output files: out/sampleFASTQs.csv
    resources: tmpdir=/tmp, mem_mb=512, mem_mib=489

Traceback (most recent call last):
  File "[redacted]/tools/snakemake/scrnaseq/workflow/scripts/getFASTQs.py", line 6, in <module>
    test(snakemake.input[0])
NameError: name 'snakemake' is not defined
[Wed May 15 05:36:05 2024]
Error in rule get_samples:
    jobid: 0
    input: config/samples.csv
    output: out/sampleFASTQs.csv
    shell:
        scripts/getFASTQs.py
        (one of the commands exited with non-zero exit code; note that snakemake uses bash strict mode!)

Shutting down, this might take some time.
Exiting because a job execution failed. Look above for error message
Complete log: .snakemake/log/2024-05-15T053604.329295.snakemake.log

I can't find this error pretty much anywhere and don't understand why snakemake is not defined in the python environment automatically.


Solution

  • If you use run directive (instead of shell), then the code block will inherit everything inside the Snakefile.

    If you use script directive, then the script will inherit the snakemake object.

    Further information can be found in the docs, it might be easier if you search "run:" and "script:" (including the colon at the end).