In a Python package, I have the file structure
There is no easy way to forbid importing a global name from a module; Python simply is not built that way.
While you could possibly achieve the forbidding goal if you wrote your own
__import__ function and shadowed the built-in one, but I doubt the cost in time and testing would be worth it nor completely effective.
What you can do is import the dependent modules with a leading underscore, which is a standard Python idiom for communicating "implementation detail, use at your own risk":
import re as _re import sys as _sys def hello(): pass
While just deleting deleting the imported modules as a way of not allowing them to be imported seems like it might work, it actually does not:
import re import sys def hello(): sys print('hello') del re del sys
and then importing and using
>>> import del_mod >>> del_mod.hello() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "del_mod.py", line 5, in hello sys NameError: global name 'sys' is not defined