Tips: Reusing Custom Functions

code
python
intermediate
tips
Author

Daniel Kick

Published

July 13, 2021

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.

  1. Set up a directory containing your functions and a blank file called __init__.py.

  2. Add the directory containing your module directory to the system path (here MaizeModel instead of MaizeModel\\library). If you’re on OSX or linux you’ll probably use single forward slashes instead of double backslashes.

  3. Finally import and call your functions.

Caveats: