Mohammed
Mohammed

Reputation: 757

Call python script from command line, which exists in another directory

My project structure:

/Users/user1/home/bashScrpts/shellScript.sh 
/Users/user1/home/pyScrpts/pyScrpt.py

From the shell script I want to call a function of pyScrpt.py

Content of pyScrpt.py

def test():
return sys.argv[1]

shellScript.sh

    DATA="testXX"
    cmd="import sys;sys.path.insert(0, '/Users/user1/home/pyScrpts/pyScrpt');import pyScrpt; print pyScrpt.test()"
    xy=$(python -c  \'${cmd}\' "${DATA}")
    echo $xy

Error I am getting:

  File "<string>", line 1
   'import
SyntaxError: EOL while scanning string literal

I don't see whats going wrong here.

Can anyone help me on this??

Upvotes: 0

Views: 89

Answers (2)

galmeriol
galmeriol

Reputation: 461

You just need to replace \' in \'${cmd}\' with double quotes "${cmd}".

Also you should add import sys to your pyScrpt.py.

Upvotes: 1

Danger Zone
Danger Zone

Reputation: 159

I have never done this before, but i would hazard a guess that it may be due to the wrong function structure, it should read:

def test() return sys.argv[1]

Upvotes: 0

Related Questions