bash 如何从 python 提示符执行(不导入)python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2021345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I execute (not import) a python script from a python prompt?
提问by fortran
I need to execute a Python script from an already started Python session, as if it were launched from the command line. I'm thinking of similar to doing sourcein bash or sh.
我需要从已经启动的 Python 会话中执行 Python 脚本,就好像它是从命令行启动的一样。我正在考虑类似于source在 bash 或 sh 中做的事情。
回答by Jason Orendorff
回答by Noah
If you're running ipython(which I highly recommend for interactive python sessions), you can type:
如果您正在运行ipython(我强烈推荐用于交互式 Python 会话),您可以输入:
%run filename
or
或者
%run filename.py
to execute the module (rather than importing it). You'll get file-name completion, which is great for ReallyLongModuleName.py (not that you'd name your modules like that or anything).
执行模块(而不是导入它)。您将获得文件名补全,这对 RealLongModuleName.py 非常有用(不是您将模块命名为类似名称或任何其他名称)。

