4

I am trying to get some open source software working. It uses things that I don't have on my system (pytorch for example) and so I thought that I could try to run it on Google Colab.

When I tried to do it though, there are some python scripts that I have to run after cloning a directory from a github repository. I guess I can't run another python script from inside a Jupyter Notebook, and so I suppose that I'm trying to do something with Colab that it isn't designed to do?

Is there something available that is more like a terminal, but using the software, GPUs etc. that are available on Colab?

user1551817
  • 6,693
  • 22
  • 72
  • 109

3 Answers3

5

You can run any shell command from jupyter-like environment (which includes colab) using ! in code cell, for example

!ls

Would list all files in colab's cwd. To run python script you could do:

!python script.py

It works just like terminal (it might be python3, not sure how it's setup un colab)

Szymon Maszke
  • 22,747
  • 4
  • 43
  • 83
3

You can call your script too.

!python script.py

But you need to put the script there, probably by git clone or direct uploading.

korakot
  • 37,818
  • 16
  • 123
  • 144
  • 2
    You'll get a richer notebook experience using the `%run` magic command for running a python script, see [here](https://stackoverflow.com/a/42203142/8508004). As for getting the file, you can directly `curl` or use `wget` to bring in the script if it is public, too, see [here](https://stackoverflow.com/a/48587645/8508004). – Wayne Jan 07 '20 at 16:28
1

As Wayne mentions in the comment korakot's answer, you can use the magic command

%run 'script.py'

This also allows you to do e.g. run in the notebook's namespace by using the -i parameter

%run -i 'script.py'
Dahn
  • 1,397
  • 1
  • 10
  • 29