I need to avoid certain file to be imported elsewhere in a project.
From Preventing Python code from importing certain modules?, I have learned that
# myscript.py
import sys
sys.modules['mymodule']=None
will make any further
import mymodule
fail when the containing myscript.py is called.
However, in my use case I need to prevent mymodule from being imported in another script, by modifying only mymodule without adding anything to myscript.py
If I understood the question correctly, put this line at the beginning of your module:
#mymodule.py
if __name__!="__main__": raise ImportError("This module is not meant to be imported")
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