- Difficulty level: easy
- Time need to lean: 10 minutes or less
- Key points:
- Magic
%env --new
runs the cell in a fresh SoS environment - Magic
%env --tempdir
runs the cell in a temporary directory that will be removed after the completion of the cell - Magic
%env --expect-error
expects an error from the cell - Magic
%env --allow-error
ignores errors from the cell - Magic
%env --set KEY=VAR
set environment variable - Magic
%env --prepend-path PATH
prependPATH
to$PATH
- Magic
Sometimes when you are developing a script you would like to make sure that the script can be executed independently. In this case you can run it in a sandbox, which runs the cell in a fresh SoS dictionary.
For example, when you define a variable filename
,
you can use it in other cells
But you can not use it directly in a new SoS environment (thus cannot be used directly as a workflow step),
This would remind you that filename
is not defined in the script and you will need to define it as a parameter and pass it from command line if you would like to expect it as a complete workflow with magic %run
:
With option --tempdir
, this magic will create a temporary directory and set it as the current directory before the execution of the cell, and remove the directory after the completion of the cell. For example, when you execute a workflow to create sandbox.txt
with %env --tempdir
, this file will not exist in the current directory after the completion of the cell.
Sometimes, for example, in the documentation of SoS, we would intentionally generate an error to demonstrate a problem. The error does not matter when we execute the notebook interactively, but would stop the execution of the notebook with Run All Cells
from Jupyter, or execute the notebook in batch mode with papermill --engine sos
.
To fix this problem, as what has been done in some previous cells of this document, you can use magic %env --expect-error
, which let SoS know that the cell will return an error. The magic will return ok
if an error
is received, and actually error
if an ok
is received.
Similar to %env --expect-error
, %env --allow-error
will tolerate an error returned from the cell, so it will return ok
if the cell returns error
, or ok
.
Option --set
set environment variables for the execution of a cell, which is meant to be temporary since the variables will be unset as soon as the exection of the cell is completed.
The values of this option can be one or more of
KEY=VALUE
pair which sets value of environment variableKEY
toVALUE
KEY
which sets value of environment variableKEY
to''
If you would like to use a command in a specific path instead of the one in system $PATH
, you can use option env
for actions (see SoS actions for details). You can also modifying the PATH
of the current cell by using option --prepend-path
.
For example,
%env
and subkernels
Whereas magics %env --expect-error
and %env --allow-error
work for any kernels, %env --prepand-path
cannot change the $PATH>/code> of the subkernels which are started with their own
$PATH
upon start and cannot be changed dynamically. They work for SoS because SoS execute separate workflows for each cell.