標準ライブラリ sys で確認できる.
チェックのためだけのスクリプトを用意.
module_checker.py
import sys
import subprocess
if "subprocess" in sys.modules:
print("module already imported")
else:
print("module not imported")
$ python module_checker.py
module already imported
import sys
# import subprocess
if "subprocess" in sys.modules:
print("module already imported")
else:
print("module not imported")
$ python module_checker.py
module not imported