0

I downloaded a script written in Python, called let's say 'myScript.py', but I don't know how to run it. It has the following structure:

import numpy as np
import argparse

parser = argparse.ArgumentParser(description='manual to this script')
parser.add_argument('--file', type=str, default = None)
parser.add_argument('--timeCor', type=bool, default = False)
parser.add_argument('--iteration', type=str, default = 'Newton')
args = parser.parse_args()


def func1(file):
...

def func2(file):
...

def calculate(data, timeCor=False, iteration='Newton'):
...


if __name__ == "__main__":
    print('\n--- Calculating ---\nN file:',args.file)
    print('Time correction =',args.timeCor,
          '\nIteration strategy =',args.iteration,'\n')
    
    rawdata,data = func2(args.file)
    pos = calculate(data,timeCor=args.timeCor,iteration=args.iteration)
    for each in pos:
        print('Pos:',format(np.uint8(each[0]),'2d'),each[1:-1])
    np.savetxt('pos.csv',pos,delimiter=',')
    print('--- Save file as "pos.csv" in the current directory ---')

How can I run it from command line? And from another script? Thank you in advance!

2 Answers 2

1

If I understood your question correctly, you are asking about executing this python program from another python script. This can be done by making use of subprocess.

import subprocess

process = subprocess.run([“python3”, “path-to-myScript.py”, “command line arguments here”], capture_output=True)
output = process.stdout.decode() # stdout is bytes

print(output)

If you do not want to provide the command in a list, you can add shell=True as an argument to subprocess.run(), or you can use shlex.split().

If you are on windows, replace python3 with python.

However this solution is not very portable, and on a more general note, if you are not strictly needing to run the script, I would recommend you to import it and call its functions directly instead.

To run the python script from command line:

python myScript.py --arguments

And as @treuss has said, if you are on a Unix system (macOS or Linux) you can add a shebang to the top of the script, but I recommend to use the following instead:

#!/usr/bin/env python3

for portability sake, as the actual path of python3 may vary.

To run the edited program:

chmod +x myScript # to make file executable, and note that there is no longer a .py extension as it is not necessary
./myScript --arguments
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your explanationi. However if I try to execute from command line, I get the following error: SyntaxError: invalid syntax, TypeError: object NoneType can't be used in 'await' expression I tried both the following: python myScript.py --file="fileName.fileExtension"
@eljamba did you get the error from running myScript.py or running the other script that is running myScript.py using subprocess?
the first one you said, I tried python myScript.py --file="fileName.fileExt" and the same command with python3 instead
That would be an issue with the script itself. Maybe you can post a separate question regarding this issue.
1

Run it by executing:

python myScript.py

Alternatively, on systems which support it (UNIX-Like systems), you can add what's called a she-bang to the first line of the file:

#!/usr/bin/python

Note that /usr/bin/python should be the actual path where your python is located. After that, make the file executable:

chmod u+x myScript.py

and run it either from the current directory:

./myScript.py

or from a different directory with full or relative path:

/path/to/python/scripts/myScript.py

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.