MCPcopy Create free account
hub / github.com/ipython/ipython / set_env

Method set_env

IPython/core/magics/osm.py:466–493  ·  view source on GitHub ↗

Set environment variables. Assumptions are that either "val" is a name in the user namespace, or val is something that evaluates to a string. Usage:\\ %set_env var val: set value for var %set_env var=val: set value for var %set_env var=$val: se

(self, parameter_s)

Source from the content-addressed store, hash-verified

464
465 @line_magic
466 def set_env(self, parameter_s):
467 """Set environment variables. Assumptions are that either "val" is a
468 name in the user namespace, or val is something that evaluates to a
469 string.
470
471 Usage:\\
472 %set_env var val: set value for var
473 %set_env var=val: set value for var
474 %set_env var=$val: set value for var, using python expansion if possible
475 """
476 split = '=' if '=' in parameter_s else ' '
477 bits = parameter_s.split(split, 1)
478 if not parameter_s.strip() or len(bits)<2:
479 raise UsageError("usage is 'set_env var=val'")
480 var = bits[0].strip()
481 val = bits[1].strip()
482 if re.match(r'.*\s.*', var):
483 # an environment variable with whitespace is almost certainly
484 # not what the user intended. what's more likely is the wrong
485 # split was chosen, ie for "set_env cmd_args A=B", we chose
486 # '=' for the split and should have chosen ' '. to get around
487 # this, users should just assign directly to os.environ or use
488 # standard magic {var} expansion.
489 err = "refusing to set env var with whitespace: '{0}'"
490 err = err.format(val)
491 raise UsageError(err)
492 os.environ[var] = val
493 print('env: {0}={1}'.format(var,val))
494
495 @line_magic
496 def pushd(self, parameter_s=''):

Callers 1

envMethod · 0.95

Calls 2

UsageErrorClass · 0.90
formatMethod · 0.45

Tested by

no test coverage detected