Search code examples
pythongoogle-colaboratory

Imitate command line execution from within code


I have a python project that is intended to be executed by running the head routine from the command line, i.e.:

python ssd.py --train

However, since this is a machine learning model and I don't have a GPU, I want to run the project entirely within Google Colab. This means I can't start files using the command line (or at least I don't know how). The workaround I've chosen is to use a single notebook as the header routine.

MWE-sketch (edited):

launcher.ipynb

!cp drive/MyDrive/.../ssd.py
from ssd import SSD
SSD("train")

ssd.py

...

__init__():
   # here is code that will
   # result in an error unless
   # the code below is executed first

...

# the code below is not inside any function
if __name__ == '__main__':
    # here is code that
    # must be executed first 

If I type python ssd.py --train into the command line, the code in ssd.py proper is executed first. If I use launcher.ipnyb, the code in __init__() is executed first, resulting in an error.


Solution

  • I've gotten it to work like this:

    ssd.py

    import sys as _sys
    
    ...
    
    __init__():
       # here is the same code as before
    
    ...
    
    def set_args(args):
        _sys.argv[1:] = args
    
    def setup():
        # here is code that was previously
        # outside any function (i.e., right below)
    
    # the code below is not inside any function
    if __name__ == '__main__':
        setup()  
    

    launcher.ipynb

    !cp drive/MyDrive/.../ssd.py
    import ssd
    ssd.set_args(["--train"])
    ssd.setup()
    

    This seems like way too complex and primitive of a solution, but I guess there's nothing better?