Bash script run properly in terminal but "command not found" when running using os.system

150 views Asked by At

I'm new to bash. I have installed "seqtk" and "magicblast" in my shell calling "conda install" on terminal, and they work properly when I directly called them on terminal or write them in a bash script(test.sh) and then run that script on terminal.

This is test.sh

#!/bin/bash

var1=$(sed 's/\.fastq\.gz/\.fa/' <<< $1)
seqtk seq -a $1 > $(basename $var1)

and this would work

$./test.sh sample_folder/sample_R1.fastq.gz

But when I tried to run the script using python(test.ipynb)

import os

os.system("./test.sh sample_folder/sample_R1.fastq.gz")

it gives me "seqtk: command not found"

I searched on Google and thought it's because I installed the commands in my shell but 'os.system' would start a new shell and run the commands there, that's why test.ipynb shows "command not found" while the commands can work properly on terminal.

I'm wondering how could I solve this issue.

Thanks in advance!

I also tried using "source" to solve this by os.system("source HOME/.bash_aliases"), and this failed and shows "source: not found". I don't really know how to use this command.

2

There are 2 answers

2
pts On

A quick workaround is doing this in Python:

os.environ['PATH'] = os.pathsep.join((
    '/mnt/data/my_folder/mambaforge/bin', os.environ['PATH']))
os.system('seqtk ...')  # It will work.
os.system('./test.sh ...')  # It will work.

However, for long term maintainability, you should activate the virtual environment instead. Doing so will take care of modifying PATH and other relevant environment variables. See the answer by @KurtisRader for details.

2
Kurtis Rader On

I see you named the file containing your Python program test.ipynb. Which suggests you are running it in a Jupyter notebook or some other IDE. Mamba (like Anaconda) is a virtual environment manager. You have to activate a virtual environment to use it. Activation means updating env variables; most importantly PATH which is used to locate external commands. Env vars are private to each process so when you activate a Mamba environment in one shell it does not activate it in any other process.

You could do what @pts suggested in their answer but I recommend not doing that. It's brittle and just introduces another source of confusing behavior. You will be better served by changing your workflow to launch your IDE from the shell where you activated the Mamba environment and otherwise learn to run any program that depends on a Mamba environment from that shell.