直接从命令行运行 Python 函数
首先,将你的函数保存在 Python 脚本文件中,例如hello.py
:
# hello.py
def hello():
return 'Hi :)'
if __name__ == '__main__':
print(hello())
``
要从命令行运行此脚本,请导航至 director
python hello.py
这
Hi :)
if __name__ == '__main__':
: 这hello()
获取print(hello())
:hello()
功能如果你只想运行该函数而不修改脚本,你可以将其导入 Python 交互式 shell 或python -c
从
例如:
python -c "from hello import hello; print(hello())"
``
这也将
Hi :)