So I have the following directory structure
- my_dir
- my_mod
__init__.py
myfile.py
- my_mod
and in myfile.py
I have a nice command line interface via argparse.
But what if sometimes I want to import a function from this script and use it as if it were a module?
e.g. from myfile import myfunc
when I do this I get an error, mostly because I do some preprocess of the command line arguments which are all none in this case.
from this S.O. post, I know how I can tell if python is called from the command line:
import sys, os
if os.isatty(sys.stdin.fileno()):
# do things
The inclusions of this line and corresponding replacement of my argument preprocessing does not solve this issue.
What should I do?