conchoecia
conchoecia

Reputation: 529

Force snakemake to run python2 in a rule without --use-conda

Problem:

From within snakemake (python3), I am running some software written in python2. Snakemake runs the software using python3, ignoring the shebang on the file invoking python2. This is someone else's software, so I don't want to bother rewriting it all to work in python3.

Question:

How do I force snakemake to run the python version in the shebang of an external script, rather than the python version in the current env?

Caveats:

I know that snakemake allows the user to force a specific environment with --use-conda, and calling a python2 environment. However, I don't want to do this, as the resulting script will be less portable.

What I have tried:

I have tried prepending the external python scripts with #!/usr/bin/env python2 or #!/usr/bin/python2, and each time the scripts failed because Snakemake ran them with python3 instead of python2. Other programs with either of these shebangs runs in python2 when I invoke the script from the shell, outside of snakemake.

Example:

This is the snakefile:

#snakefile
rule all:
    input:
        my_output.txt
rule foo:
    output:
        txt = my_output.txt
    shell:
        """
        external_program.py > {output.txt}
        """

This is external_program.py:

#!/usr/bin/env python2

print "this will work in python2, but not python3"

Upvotes: 1

Views: 755

Answers (1)

Maarten-vd-Sande
Maarten-vd-Sande

Reputation: 3701

I really think you should solve this by using conda environments. I think your current approach is the "less portable" one. So I decided to give two answers in this question, one with, and one without, conda> In the hope that I would convince you to use the conda approach :).

without conda

If python 2 is installed on your computer, then you can probably invoke it with either python2 or /usr/bin/python2

rule foo:
    output:
        txt = my_output.txt
    shell:
        """
        python2 external_program.py > {output.txt}
        """

with conda

Solving this with conda would require us making a environment.yaml file:

dependencies:
  - python=2.7

and now we have to refer to this environment like so:

rule foo:
    output:
        txt = my_output.txt
    conda: "environment.yaml"
    shell:
        """
        python2 external_program.py > {output.txt}
        """

and use the --use-conda flag with snakemake.

Upvotes: 1

Related Questions