Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in Importing a bash script into a python

I am trying to create a python script script.py in bash and importing a bash script.

#!/usr/bin/env python    
import os
import glob
from fnmatch import fnmatch
# importing a software
python_package = os.system("""#!/path_to_bin/bin/python \
from __future__ import print_function, division \
from python_toolbox.toolbox.some_toolbox import run \
if __name__ == '__main__': \
    run()"""

# testing 
greeting = "Hello world!"
print(greeting)

Running the script.py in python3

$python3 script.py
  File "script.py", line 15
    greeting = "Hello world!"
SyntaxError: invalid syntax

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Nominally the problem is that you are missing the closing paren on the os.system call. But there is a better way to run a python program than trying to write it all on the command line. Instead, you can pass a full script, including newlines, to python’s stdin.

#!/usr/bin/env python    
import sys
import subprocess as subp

# importing a software
subp.run([sys.executable, "-"], input=b"""
print("I am a called python script")
""")

# testing 
greeting = "Hello world!"
print(greeting)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading