Macaulay2 » Documentation
Packages » Python » pythonRunScript
next | previous | forward | backward | up | index | toc

pythonRunScript -- execute a sequence of Python statements

Description

Execute a sequence of statements as if they were read from a Python file. This is for multi-line code that might contain definitions, control structures, imports, etc. It is great for running Python code from a file.

The return value is a Python dictionary containing all the variables defined in the global scope.

i1 : pyfile = temporaryFileName() | ".py"

o1 = /tmp/M2-52249-0/0.py
i2 : pyfile << "import math" << endl

o2 = /tmp/M2-52249-0/0.py

o2 : File
i3 : pyfile << "x = math.sin(3.4)" << endl << close

o3 = /tmp/M2-52249-0/0.py

o3 : File
i4 : get pyfile

o4 = import math
     x = math.sin(3.4)
i5 : pythonRunScript oo

o5 = {'math': <module 'math' (built-in)>, 'x': -0.2555411020268312}

o5 : PythonObject of class dict

The Global option may be used to pass a globals dictionary so that variables may be shared between scripts.

i6 : pythonRunScript("y = math.cos(x)", Global => oo)

o6 = {'math': <module 'math' (built-in)>, 'x': -0.2555411020268312, 'y':
     0.9675266635316214}

o6 : PythonObject of class dict

See also

Ways to use pythonRunScript:

  • pythonRunScript(Sequence)
  • pythonRunScript(String)

For the programmer

The object pythonRunScript is a method function with a single argument.


The source of this document is in Python/doc.m2:270:0.