Sometimes you want to share functions across notebooks and scripts. You can do this by putting your common functions in a package. Compared to a public package, you do not need to specify as much information for a private package.
Step 1: Put setup.py
in the repository folder
from setuptools import setup
setup()
Step 2: Put setup.cfg
in the repository folder
Here is an example setup.cfg
for a private package. Note that we specified the sources
folder as containing our package(s).
[metadata]
name = your-package
version = 1.2.3
[options]
packages = find:
install_requires =
crosscompute >= 0.9.4.9
zip_safe = True
[options.extras_require]
development =
pre-commit
[options.packages.find]
where = sources
[options.package_data]
your_package =
assets/*.html
[flake8]
max-line-length = 79
select = B,C,E,F,W
ignore = W503
Step 3: Collect common functions
# Make package folder
mkdir sources/your_package -p
# Make package importable
touch sources/your_package/__init__.py
# Put your common functions into the package
vim sources/your_package/routines.py
def f(x, y):
return x + y
Step 4: Install your package in editable mode
pip install -e .
Step 5: Import functions from package in notebooks and scripts
from your_package.routines import f
f(1, 2)