I've got a python script, which should read a subdirectory and make all the scripts within it callable like this:
python mainscript.py subscript arguments
I've figured out most of the details but I'm having a lot of trouble deciding on how to export an API (object or selected functions) to
mainscript imports. The intention is for the
subscripts to work as pluggable commands to
mainscript (in other words, there is no need for them to work stand-alone).
Things I've tried, that I think would be elegant enough are (after
import subdirectory.subscript in
which works, but gives my subscript access to everything unconditionally and will likely be a source of horrible bugs if this is to be used by others.
from __main__ import *
which doesn't work despite defining
__all__to include the name
apiat the end of
mainscript(I just get told that
name 'api' is not definedwhen I try to reference the object I want).
from __main__ import api
which tells me
cannot import name api.
So I'm at a position where, I either do the haphazard
import __main__ which I don't like at all, or have to move the api to a different file, which I also dislike because it would break the intended structure of:
mainscript.py - subdirectory: - subscript: subscript.py
__init__.py files that have to be everywhere along the chain, is relatively simple to work with. An
api.py somewhere would mean that, to maintain simplicity, I'd have to move
api.py into their own subdirectory and have a calling script where
mainscript.py used to be (a python script, to keep things easily cross-platform, but that just adds another script to the chain). I want to keep everything simple and obvious, which is why I'd prefer to have the simple api within the master caller script.
In summary: What is the recommended, if any, way to export an api object from a calling script to called scripts? Is this approach to exporting an api wrong?