Siva kumar Reddy Siva kumar Reddy - 1 year ago 54
Python Question

how can i control modules in package to import in python?

Consider an example,

I have a package having list of modules:

: I would like to allow only
: allow

If I write,
import mypackage
# only mod2 should import
import mypackage
# only mod1,mod3 should import

How can I restrict at package or module level?

Answer Source

I don't think that packages should control who and how can import them, basically packages should not know about their importers. However if you for some reason still thing this is a good idea, you can get a main filename by:

import __main__
main_file = __main__.__file__

And then modify your


attribute of module based on a main file name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download