Re-using prior work is an important part of maintaining a solid and reliable code-base by reducing redundancy/duplication and often overall complexity.
The most powerful mechanism for re-using existing code, short of having one monolithic module, is to use Python's import machinery.
We can import any module (e.g. just a .py file) if it's parent directory is on our sys.path (also controlled with the PYTHONPATH env variable, amongst other things). By default, the CWD is on the sys.path, therefore we can automatically import our .py files (by their filename minus the .py) if we are developing a module in the same directory as our previous work.
Often this is a good enough solution for our ad-hoc work, but sometimes it is valuable to bundle up our work into a convenient package (which is just a collection of modules). This is especially true if we are writing something broadly useful that other people (including ourselves) may wish to make use of as a user.
The simplest package is just a single module (.py file), but as our package develops it can quickly grow-out of the single file model. To implement a multi-module package, we simply create a folder with a __init__.py module within. Again, assuming the package (or precisely the parent folder of our package folder) is on the sys.path, we can simply import our functionality with import my_package.
The python setuptools documentation at https://pythonhosted.org/setuptools/setuptools.html#basic-use covers a lot of detail about making your package installable using a setup.py. However, the simplest setup.py looks like:
from setuptools import setup
setup(
name = "DistributionName",
version = "0.1",
packages = ['package_name'],
)
Exercise:
my_package (the package name), within it put an empty __init__.py file.setup.py next to the my_package folder with appropriate information (Distribution name: MyDistribution; packages: my_package;).my_package in developer mode by running python setup.py develop --user.python -c "import my_package; print my_package.__file____init__.py file in my_package (a function which prints "hello world" is fine). Assert that when my_package is imported, we can access this function as my_package.my_function.
In [1]:
%run resources/load_style.py