Amendment: For packaging functions also see nbdev.
I wanted to reuse a custom function across a few scripts without having copies of the same code in each script. The solution I found is to set up a module to hold these functions. This seems straightforward once you know how it’s done.
Set up a directory containing your functions and a blank file called
__init__.py
.Add the directory containing your module directory to the system path (here
MaizeModel
instead ofMaizeModel\\library
). If you’re on OSX or linux you’ll probably use single forward slashes instead of double backslashes.Finally import and call your functions.
Caveats:
- It seems that the system path isn’t permanently altered by
sys.path.append
, so one would need that at the start of the script or modify it some other way. - If your custom functions are in the in the same directory as your script, I think you can skip all of this and just import them.
- If your functions are in a sub-directory of the same directory as your script, I think you can get away without adding the directory to the path.