CuriousDude
CuriousDude

Reputation: 1117

Python script won't run in bash script, but works fine in cmd prompt

I have a python script that I want to automate so I created a simple bash script (called mybash.sh).

#!/bin/sh

vars="C:\Users\Jane\Desktop\Work\variables.csv"

while IFS="," read gname gid
do
    echo "Gene Name: $gname"
    echo "Gene ID: $gid"
    python alignment.py work_file $gid > C:\\Users\\Jane\\Desktop\\Work_Done\\$gname.fa
done < "$vars"

read -rn1

It is a huge script but I always get an error saying that the last line of my alignment.py script has a NameError, so after running my bash script from windows cmd prompt as >mybash.sh I get this:

Gene Name: cytb   
Gene ID: ENSB0010011
Traceback (most recent call last):
  File "alignment.py", line 99, in <module>
    for species in geneDict:
NameError: name 'geneDict' is not defined

The geneDict is the very last part of the python script which simply creates a fasta alignment file for a gene for all species in separate folder. When I execute the python script normally in cmd prompt, it works perfectly fine but instead of extracting the gname (gene name) and gid (gene ID) from a variables file, I was typing it manually. I didn't want to do that for 200 genes. I can't understand why it no longer finishes and says that the geneDict is no longer defined?

Also, I tried making the python script run in a bash script alone as follows:

#!/bin/sh

python alignment.py work_file ENSB0010011 > C:\\Users\\Jane\\Desktop\\Work_Done\\cytb.fa

And this worked fine also, the python script did not stop working all of a sudden and my file came out. How do I make the python script execute without error, while pulling the variables from variables.csv separately so that I don't have to type it in manually?

The last bit of the python script is as follows:

for species in geneDict:    # this is where it says geneDict is not defined
    print '>' + species
    print geneDict[species]

Upvotes: 0

Views: 689

Answers (1)

Elis Byberi
Elis Byberi

Reputation: 1452

NameError exception is raised because global variable "geneDict" is not defined in python script. See https://docs.python.org/2/library/exceptions.html#exceptions.NameError

Upvotes: 1

Related Questions