I am trying to organize some modules for my own use. I have something like this:
lib/ __init__.py settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py
In lib/__init__.py, I want to define some classes to be used if I import lib. However, I can’t seem to figure it out without separating the classes into files, and import them in__init__.py.
Rather than say:
lib/ __init__.py settings.py helperclass.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py from lib.settings import Values from lib.helperclass import Helper
I want something like this:
lib/ __init__.py #Helper defined in this file settings.py foo/ __init__.py someobject.py bar/ __init__.py somethingelse.py from lib.settings import Values from lib import Helper
Is it possible, or do I have to separate the class into another file?
EDIT
OK, if I import lib from another script, I can access the Helper class. How can I access the Helper class from settings.py?
The example here describes Intra-Package References. I quote ‘submodules often need to refer to each other’. In my case, the lib.settings.py needs the Helper and lib.foo.someobject need access to Helper, so where should I define the Helper class?
‘
lib/‘s parent directory must be insys.path.Your ‘
lib/__init__.py‘ might look like this:Then the following example should work:
Answer to the edited version of the question:
__init__.pydefines how your package looks from outside. If you need to useHelperinsettings.pythen defineHelperin a different file e.g., ‘lib/helper.py‘.The command:
Output: