How to import everything from a python namespace / package?


It is a bad idea to be importing everything from a Python package as a package is not a super-module -- it's a collection of modules grouped together. So you should just import what you need in that file. Also importing everything from package into your global namespace is going to cause a proliferation of names, and very likely conflicts among those names.

That being said, there are still ways to do this. First one being manually importing everything from a package using import statements for every sub-module.  Another way, as the documentation at http://docs.python.org/tutorial/modules.html#importing-from-a-package - suggests, is that if you have a string list named __all__ in your __init__.py file, all module/sub-package names in that list are imported when one does from pack import *. So you will need to create a list of strings of everything in your package and then do a "from packageName import *" to import everything in this module so when you import this elsewhere, all those are also imported within this namespace.

Updated on: 30-Jul-2019

504 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements