Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python modules: how to keep private symbol table private?

Tags:

python

module

I have a module in which I import other modules (typically sys or os), and when I import the module with

import mymodule

if I do something like this

dir(mymodule)

I get not only the submodules of the module, but also os et al.

['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'os', 'sys', 'submodule']

Is there a way to avoid to have the modules imported inside my module to show up in the symbol table of the module? Is it possible to keep it private?

like image 281
MBR Avatar asked Jun 04 '26 23:06

MBR


1 Answers

Well, you can delete the module from the namespace using del:

import os

# ... all other code here

del os

After that it won't be visible anymore when importing.

However, this won't work when the modules are actually used...

like image 174
Simeon Visser Avatar answered Jun 06 '26 12:06

Simeon Visser