0

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??

4
  • thanks for the reply but if want to run python script inline from bash I need single quotes. I tried double quotes also but facing same issue. Commented Apr 9, 2018 at 15:02
  • then this faq? Commented Apr 9, 2018 at 15:12
  • One more problem: Since you import pyScrpt, sys.argv[1] will generate an IndexError Commented Apr 9, 2018 at 15:22
  • I guess it wont complain @Hai because I am passing the arg to it Commented Apr 9, 2018 at 15:36

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

thanks!!!! that solved the error but getting the following error now: ImportError: No module named pyScrpt
You have to change the path for python script; sys.path.insert(0, '/Users/user1/home/pyScrpts/pyScrpt') to sys.path.insert(0, '/Users/user1/home/pyScrpts')
why is that? what should give as a path then?
0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.