Using module's own objects in __main__.py
问题 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 ? 回答1: You need to either have the package already in sys.path , add the directory containing mymod to sys.path in __main__.py , or use the -m switch. To add mymod to the path would look something like this (in __main__.py ): import sys