Recommended Answers
Jump to PostCan you post your script? It depends on what you want to do. For example, here is a function which prints hello world
#!/usr/bin/env python # -*-coding: utf8-*- # This is file hello.py # Line 1 above tells the linux shell that this # program must be …
Jump to PostThen the script must take an argument which is the function name. The best thing to do is to use the argparse module to parse the command line, something like
#!/usr/bin/env python3 # -*-coding: utf8-*- def sayhi(): print('hi everybody!') if __name__ == "__main__": import argparse parser = …
Jump to PostIf you saved your file as hello.py somewhere Python looks for, do this from the shell:
>>> import hello >>> hello.thefunc()
Jump to PostOr simpler:
>>> execfile("hello.py")
Jump to PostUse argparse ! It is by far the best way to do this
#!/usr/bin/env python3 # -*-coding: utf8-*- def sayhi(): print('hi everybody!') def goodbye(): print('see you soon!') if __name__ == "__main__": import argparse parser = argparse.ArgumentParser(description='Execute a function') parser.add_argument('funcname', help='name of function to execute', metavar='FUNCNAME', choices=['goodbye', 'sayhi']) …
All 20 Replies
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.