Is it possible to import
everything (*
) from an existing Python module except a number of explicitly specified methods?
(Background: Against recommended Python practice it is common in FEniCS to do from dolfin import *
. A few of the methods names contain the string "Test" though (e.g., TestFunction()
) and are mistaken for unit tests by nose.)
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.
You can choose to import only parts from a module, by using the from keyword.
The difference between import and from import in Python is: import imports the whole library. from import imports a specific member or members of the library.
importlib allows you to import any Python module from a string name. You can automate it with going through the list of files in the path. It's more pythonic to use __all__ . Check here for more details.
In case you don't have an access to the module, you can also simply remove these methods or variables from a global namespace. Here's how this could be done:
to_exclude = ['foo']
from somemodule import *
for name in to_exclude:
del globals()[name]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With