0

Currently executing this in google colab:

!python myfile.py   --size 45  --file textfile.txt --data_folder somePath/folder

How can I put the above command into a python file (executeNow.py), so that I'll be able to run this in google colab:

!python executeNow.py

Namely what should be written inside execute.py so that the above to commands will be equivalent?

1 Answer 1

1

You can use the module subprocess for that. Put this in your executeNow.py:

import subprocess
subprocess.run(["python", "myfile.py", "--size", "45", "--file", "textfile.txt", "--data_folder", "somePath/folder"])

However, for this task I'd recommend using a bash script: write this into executeNow.sh:

#!/bin/bash
python myfile.py --size 45 --file textfile.txt --data_folder somePath/folder

Optionally, make that file executable:

!chmod +x executeNow.sh

Then run this in Colab:

!sh executeNow.sh

If you did the optional part, you can contract the above command slightly shorter:

!./executeNow.sh
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your reply! But I need to execute it inside the py file. So I'm looking for a way to do that.
@DannyDan if that's what you need, I've updated my answer accordingly.
may you please help with: %pip install -qr requirement.txt

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.