Python:从命令行运行函数 [英] Python: Run function from the command line

查看:496
本文介绍了Python:从命令行运行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件中有以下代码:

I have below code in my file:

def hello():
    return 'Hi :)'

我如何从命令行运行?

推荐答案

使用 -c (命令)参数(假设您的文件名为 foo.py ):

$ python -c 'import foo; print foo.hello()'

或者,如果你不在乎命名空间污染: p>

Alternatively, if you don't care about namespace pollution:

$ python -c 'from foo import *; print hello()'

中间地:

$ python -c 'from foo import hello; print hello()'

这篇关于Python:从命令行运行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆