Calling a python function from bash script

邮差的信 提交于 2019-12-20 10:27:39

问题


I have an "alarm email" function inside a python module. I want to be able to call this function from a bash script. I know you can call a module using 'python ' in the script, but I'm not if you can or how you would call a specific function within the module.


回答1:


python -c'import themodule; themodule.thefunction("boo!")'



回答2:


You can use the -c option:

python -c "import random; print random.uniform(0, 1)"

Modify as you need.




回答3:


To call a specific function in a module, assure that the module has the following:

if __name__ == "__main__":
    the_function_to_call( )

Then you can simply do this in your shell script.

python module.py

Or

python -m module

Depending on whether or not the module's on the PYTHONPATH.



来源:https://stackoverflow.com/questions/2119702/calling-a-python-function-from-bash-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!