I’m trying to access a module’s data from inside its __main__.py.
The structure is as follows:
mymod/
__init__.py
__main__.py
Now, if I expose a variable in __init__.py like this:
__all__ = ['foo']
foo = {'bar': 'baz'}
How can I access foo from __main__.py?
You need to either have the package already in
sys.path, add the directory containingmymodtosys.pathin__main__.py, or use the-mswitch.To add
mymodto the path would look something like this (in__main__.py):Using the
-mswitch would like:See this answer for more discussion.