Remove imported modules from python
Question:
If I have a python file, named “xtraimport.py” with this in it:
import os
import sys
def its_true():
return True
When I import it, it contains the symbol “os” and “sys”:
In [3]: import xtraimport
In [4]: dir(xtraimport)
Out[4]:
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'its_true',
'os',
'sys']
Is there any way to remove the name space pollution? I understand the symbols were imported in my module, but I don’t want to expose them to everybody that uses the library.
Answers:
Easier? Probably not. But here’s a solution if you like it.
import xtraimport
import sys
originalDir = dir(xtraimport)
newDir = [x for x in originalDir if x not in sys.modules]
Take a look here for more detail on sys.modules
: sys.modules
One way to get some control over this is to make your module into a package. On the python path, create the xtraimport
. In that directory place two files: (1) xtraimport.py
and (2) __init__.py
. For the contents of __init__.py
, use:
from xtraimport import its_true
In this way, only its_true
is directly exposed:
In [1]: import xtraimport
In [2]: dir(xtraimport)
Out[2]:
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'__path__',
'its_true',
'xtraimport']
The full contents of xtraimport
are still available for the dedicated users who are willing to take an extra step:
In [3]: dir(xtraimport.xtraimport)
Out[3]:
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'its_true',
'os',
'sys']
Setting __all__
allows you to specify which names you would like your module’s public interface to contain. It doesn’t actually prevent the others from being accessed, but it affects from foo import *
, pydoc, and it might affect autocomplete depending on the autocomplete software.
E.g.:
__all__ = ["my_function", "MyClass"]
To remove an imported module, for example, if I am going to remove the imported cv2 module, I can do so (Python 3.9):
I can see cv2 has been imported with:
print(dir())
Now to remove cv2:
del cv2
Check it again:
print(dir())
If I have a python file, named “xtraimport.py” with this in it:
import os
import sys
def its_true():
return True
When I import it, it contains the symbol “os” and “sys”:
In [3]: import xtraimport
In [4]: dir(xtraimport)
Out[4]:
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'its_true',
'os',
'sys']
Is there any way to remove the name space pollution? I understand the symbols were imported in my module, but I don’t want to expose them to everybody that uses the library.
Easier? Probably not. But here’s a solution if you like it.
import xtraimport
import sys
originalDir = dir(xtraimport)
newDir = [x for x in originalDir if x not in sys.modules]
Take a look here for more detail on sys.modules
: sys.modules
One way to get some control over this is to make your module into a package. On the python path, create the xtraimport
. In that directory place two files: (1) xtraimport.py
and (2) __init__.py
. For the contents of __init__.py
, use:
from xtraimport import its_true
In this way, only its_true
is directly exposed:
In [1]: import xtraimport
In [2]: dir(xtraimport)
Out[2]:
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'__path__',
'its_true',
'xtraimport']
The full contents of xtraimport
are still available for the dedicated users who are willing to take an extra step:
In [3]: dir(xtraimport.xtraimport)
Out[3]:
['__builtins__',
'__doc__',
'__file__',
'__name__',
'__package__',
'its_true',
'os',
'sys']
Setting __all__
allows you to specify which names you would like your module’s public interface to contain. It doesn’t actually prevent the others from being accessed, but it affects from foo import *
, pydoc, and it might affect autocomplete depending on the autocomplete software.
E.g.:
__all__ = ["my_function", "MyClass"]
To remove an imported module, for example, if I am going to remove the imported cv2 module, I can do so (Python 3.9):
I can see cv2 has been imported with:
print(dir())
Now to remove cv2:
del cv2
Check it again:
print(dir())