Edrich
Edrich

Reputation: 19

Run .PY Script Located in GCS

Can you run a script that's located in Google Cloud Storage? For example, lets say I have a script in a bucket called "bucket01" GCS:

-- bucket01/the-script.py

And all the-script.py does is return a "Hello World" string. Can I run that script from a Datalab notebook? If so, how? If not, what practice should I implement?

Upvotes: 1

Views: 1621

Answers (1)

Nikhil Kothari
Nikhil Kothari

Reputation: 5225

You'll need to pull down the script from GCS using gsutil or the %storage commands, and then run.

This is because python itself doesn't know about GCS and is unable to treat it as a file system to read from.

So try something like this:

!gsutil cp bucket01/the-script.py the_script.py

And then run either directly:

!python the_script.py

Or import into the notebook:

import the_script

In general you may also want to consider turning your script into a python module that you can install via pip, as you may eventually find yourself needing more than the one .py file, and at that point it is simpler to think of whole libraries that you pull down into your Datalab instance rather than separate files.

Hope that helps.

Upvotes: 1

Related Questions