Running Python code¶
Run the examples¶
To run the following examples, load
Execute Python code¶
The interface allows execution of Python code directly in a q console or from a script. In both console and scripts, prefix Python code with
Q scripts (but not the console) can load and execute multiline Python code. Prefix the first line of the code with
p) and indent subsequent lines of Python code according to the usual Python indentation rules.
$ cat embedPytest.q a:1 / q code p)def add1(arg1): # Python code return arg1+1 # still Python code
In a q session
q)\l embedPytest.q q)p)print(add1(12)) 13
Full scripts of Python code can be executed in q, using the
.p file extension (not
.py). The script is loaded as usual.
$ cat helloq.p print("Hello q!")
q)\l helloq.p Hello q!
Evaluate Python code¶
To evaluate Python code (as a string) and return results to q, use
Side effects Python evaluation (unlike Python execution) does not allow side effects. Any attempt at variable assignment or class definition will signal an error. To execute a string performing side effects, use
.p.e. A more detailed explanation of the difference between
exec in Python can be found here.