xaviersjs
xaviersjs

Reputation: 1737

How can I discover what package __main__ belongs to

suppose I invoke a script thusly

python -m some_package.script

Is there a way to programmatically identify that __main__ belongs to some_package?

Obviously I can't rely on __name__ because it's always the same value. I haven't figured out if I can rely on sys.argv[0] because there might be an arbitrary number of directories before I get to a PYTHONPATH variable.

Is there some way to discover the package to which a main script belongs?

Upvotes: 1

Views: 58

Answers (1)

xaviersjs
xaviersjs

Reputation: 1737

Apparently it's

sys.modules['__main__'].__package__

Upvotes: 2

Related Questions