pyexec — Execute a script from a file at k-time or i-time (i suffix).
pyexec "filename"
pyexeci "filename"
pylexec "filename"
pylexeci "filename"
pyexect ktrigger, "filename"
plyexect ktrigger, "filename"
Plugin opcode in py. This opcode is part of the plugin repository and has to be installed separately. The plugin repository can be found here: https://github.com/csound/plugins
Execute a script from a file at k-time or i-time (i suffix).
This is not the same as calling the script with the system()
call, since the code is executed by the embedded interpreter.
The code contained in the specified file is executed in the global environment for opcodes pyexec and pyexeci and in the private environment for the opcodes pylexec and pylexeci.
These opcodes perform no message passing. However, since the statement has access to the main namespace and the private namespace, it can interact with objects previously created in that environment.
The "local" version of the pyexec opcodes are useful when the code ran by different instances of an instrument should not interact.
Example 868. Orchestra (pyexec.orc)
sr=44100 kr=4410 ksmps=10 nchnls=1 ;If you are not running CsoundAC you need the following line ;to initialize the python interpreter ;pyinit pyruni "import random" pyexeci "pyexec1.py" instr 1 pyexec "pyexec2.py" pylexeci "pyexec3.py" pylexec "pyexec4.py" endin
Example 870. The pyexec1.py Script
import time, os print print "Welcome to Csound!" try: s = ', %s?' % os.getenv('USER') except: s = '?' print 'What sound do you want to hear today%s' % s answer = raw_input()
If I run this example on my machine I get something like:
Using ../../csound.xmg Csound Version 4.19 (Mar 23 2002) Embedded Python interpreter version 2.2 orchname: pyexec.orc scorename: pyexec.sco sorting score ... ... done orch compiler: 11 lines read instr 1 Csound Version 4.19 (Mar 23 2002) displays suppressed Welcome to Csound! What sound do you want to hear today, maurizio?
then I answer
a sound
then Csound continues with the normal performance
your answer is "a sound" a private random number: 0.884006 new alloc for instr 1: your answer is "a sound" a private random number: 0.884006 your answer is "a sound" a private random number: 0.889868 your answer is "a sound" a private random number: 0.884006 your answer is "a sound" a private random number: 0.889868 your answer is "a sound" a private random number: 0.884006 your answer is "a sound" ...
In the same instrument a message is created in the private namespace and printed, appearing different for each instance.