Skip to content

Instantly share code, notes, and snippets.

@danielballan
Created September 2, 2020 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielballan/3db762de0158c85d502d48a52294d1d1 to your computer and use it in GitHub Desktop.
Save danielballan/3db762de0158c85d502d48a52294d1d1 to your computer and use it in GitHub Desktop.
testing main access from in process

Put these in a directory together and run:

python test.py
#!/usr/bin/python3
import argparse
def main(args_=None):
"Use args_ to pass in arguments like ['foo', 'bar']"
parser = argparse.ArgumentParser()
parser.add_argument('a', type=str)
parser.add_argument('b', type=str)
args = parser.parse_args(args_)
print(f"args={args}")
if __name__ == '__main__':
main()
from example import main
print("Called from this process via Python API:")
main(["this", "process"])
print("Called from subprocess via CLI:")
import subprocess, sys
subprocess.run(["python", "example.py", "another", "process"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment