I would like to import functions which are stored in separate .py files. Example:
I try to follow the turorial at
In the file: nyabinghi.py the following def is stored:
def nyabinghi():
# low conga
cl >> play("cc00", dur=1, sample=(0), pan=-0.5,room=0.3, verb=0.3, sus=0.5)
# open hi conga
hc >> play(PEuclid2(1,8,'0','c'), dur=1, sample=(4), pan=-0.55,room=0.5, verb=0.3, sus=0.5)
# bongo low
bl >> play("bb00", dur=1, sample=(1), pan=-0.25,room=0.3, verb=0.2, sus=0.5)
# bongo hi
bh >> play("[bbb0][b000]0", dur=[1, 1, 6], sample=(0), pan=-0.3,room=0.5, verb=0.2, sus=0.5)
# crash cymba
cc >> play(PEuclid2(1,16,'0','C'), dur=1, sample=(0), amp=0.2, pan=0.5,room=0.0, verb=0.0, sus=0.2)
# cowbell
cb >> play("[T0T0][T000]0", dur=[1, 1, 6], sample=(1), amp=0.1, pan=0.3,room=0.3, verb=0.2, sus=0.5)
# hi-mid-tom
ht >> play("00M", dur=[2, 0.6, 1.4], sample=(4), pan=0.2, room=0.5, verb=0.5, sus=2)
# low-mid-tom
mt >> play("00M0", dur=[3, 5], sample=(1), pan=0.2, room=0.5, verb=0.5, sus=2)
# open hi-hat
oh >> play("00=", dur=[0.1, 3.9, 4], sample=(1), pan=0.45, room=0.5, verb=0.5, sus=2)
# low tom
lt >> play("0mm0", dur=[0.1, 3.5, 0.4, 4], sample=(0), pan=0.2, room=0.5, verb=0.5, sus=2)
# close hi-hat
ch >> play("00--", dur=1, sample=(0), pan=0.45, room=0.5, verb=0.5, sus=0.5)
# bass drum
bd >> play("X000", dur=1, sample=(0), pan=0.4, room=0.7, verb=0.7, sus=3)
nyabinghi = Group(cl,hc,bl,bh,cc,cb,ht,mt,oh,lt,ch,bd)
return nyabinghi
When I try to import the nyabinghi() function in the file create_reggae_music.py which is stored in the same directory like so:
from nyabinghi import nyabinghi
I get the following error message:
ModuleNotFoundError: No module named ‘nyabinghi’
If I copy and paste the code into the create_reggae_music.py file the function works as expected with:
def csr(bpm=140, scale="chromatic",root="C"):
Clock.bpm = bpm
Scale.default = scale
Root.default = root
return
csr()
nyabinghi_group = nyabinghi()
As I modified (added some wav files to) my snd directory, the result might sound funny if you don’t adjust the samples folder, but the rhythm should be right
As my project becomes more complex I’d like to (re)use my own external modules with the import function. I am working on windows 10 with FoxDot v.0.8.12 and SC 3.11.2.
How could I Import my own function modules correctly?
I found the following solution at
How to include external Python code to use in other files? - Stack Overflow
using an absolute path for the nyabinghi.py file:
# Windows: with open(r'E:\FoxDot\own_files\nyabinghi.py') as f: exec(f.read())
after executing the include line above, functions and constants, defined in nyabinghi.py could be called from within the FoxDot Editor window like:
nyabinghi()
The function call is executed as expected.
I tried the same with Debian OS and it works fine using an absolute path (with slash instead ob backslash) for Linux.
Although I could import external Python modules like:
from collections import deque
I was not able to import own modules with the import command (as described above). Using an absolute path didn’t work for me too. I still would appreciate a hint how the import command for own .py files works from within the FoxDot editor.