如何从python提示符执行(不导入)python脚本?

for*_*ran 6 python bash import module

我需要从已经启动的Python会话中执行Python脚本,就像它是从命令行启动一样.我在考虑类似于source在bash或sh中做.

Jas*_*rff 11

在Python 2中,内置函数execfile执行此操作.

execfile(filename)
Run Code Online (Sandbox Code Playgroud)

  • 你必须爱python.你怎么*在python:execfile中执行*文件*. (2认同)
  • 是的,`execfile()`在Python 3中消失了.请参阅[Python 3.0中execfile的替代方法是什么?](http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile -in-蟒-3-0) (2认同)