Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import everything from a module except a few methods

Tags:

python

import

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.)

like image 445
Nico Schlömer Avatar asked Nov 13 '13 15:11

Nico Schlömer


People also ask

How do I import everything into Python?

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.

Which of the following can be used to import only part of a module?

You can choose to import only parts from a module, by using the from keyword.

What is the difference between from import and import?

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.

How can you import all functions from Python file in a previous dictionary?

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.


1 Answers

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]
like image 190
Alexander Zhukov Avatar answered Sep 20 '22 05:09

Alexander Zhukov