<jupyter_start><jupyter_text>Anything you `export` gets written to your `.py` - so you're free to use `if __name__ == "__main__":` etc<jupyter_code>#export
if __name__ == "__main__":
    print("Run when invoked directly.")
else:
    print("Run when imported.")<jupyter_output>Run when invoked directly.<jupyter_text>It might be a problem that `__name__ == "__main__"` when running in a notebook.`IN_NOTEBOOK` can tell us if we're running in a notebook or not:<jupyter_code>#export
try: from nbdev.imports import IN_NOTEBOOK
except: IN_NOTEBOOK=False
print("Running in notebook" if IN_NOTEBOOK else "Not running in notebook")<jupyter_output>Running in notebook<jupyter_text>so you could do something like:<jupyter_code>#export
if __name__ == "__main__" and not IN_NOTEBOOK:
    print('Running "command line" logic ...')
from nbdev.export import notebook2script
notebook2script('72_if__name__.ipynb')
!python decision_tree/test/expif.py 
import decision_tree.test.expif<jupyter_output>Run when imported.
Running in notebook
{'fraction_non_alphanumeric': 0.10088148873653281, 'fraction_numerical': 0.0039177277179236044, 'mean_word_length': 6.35251798561151, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '594926', 'license': 'Apache-2.0', 'n_tokens_mistral': 347, 'n_tokens_neox': 341, 'n_words': 102, 'path': '72_if__name__.ipynb', 'repo_name': 'pete88b/decision_tree', 'size': '1021'}