1

I'm running this code for ALBERT, one of Google's machine learning models on Google Colab. At the end of the code, they do everything by running a script using ! (the shell) to run the script. However, I'd like to do some stuff to the resulting model after the code has run.

Is there any way to either access or get the script to output particular variables in a way that my code in Colab could access afterwards?

Here's another way of phrasing my question. Putting $HELLO_WORLD into the shell command accesses the HELLO_WORLD variable in my Colab code. Is there any way to get the script to set the HELLO_WORLD variable in my Colab code?

2 Answers 2

1

You can use os.environ like this.

import os
os.environ['HELLO_WORLD']='hello world from Python'

Then later

!echo $HELLO_WORLD

# hello world from Python
Sign up to request clarification or add additional context in comments.

Comments

0

You can assign the output of a bash command to a Python variable:

FILES = !ls -al

In any other following cell you can show FILES in Python

for f in FILES:
    print(f)
# Out: 
# total 16
# drwxr-xr-x 1 root root 4096 May 29 14:01 .
# drwxr-xr-x 1 root root 4096 Jun  2 09:56 ..
# drwxr-xr-x 4 root root 4096 May 29 14:01 .config
# drwxr-xr-x 1 root root 4096 May 29 14:01 sample_data

Execute any other process using ! with string interpolation from Python variables, and note the result can be assigned to a variable (from Google Colab documentation)

Note that this works in Google Colab as well as in Jupyter notebooks since it's a core feature of IPython (see Manual capture of command output and magic output).

💡 But if the output is large, it’s better to write it to a file in the shell and read it from Python — this keeps the notebook clean and avoids memory issues.

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.