gpt4 book ai didi

python - 如何在 Python 解释器中执行文件?

转载 作者:IT老高 更新时间:2023-10-28 12:10:28 25 4
gpt4 key购买 nike

我正在尝试使用解释器中的 Python 命令执行文件。

编辑:我正在尝试使用该文件中的变量和设置,而不是调用单独的进程。

最佳答案

几种方法。

  • 从外壳

    python someFile.py
  • 在 IDLE 中,按 F5

  • 如果您是交互式输入,试试这个 (Python3):

    >>> exec(open("filename.py").read())
  • 对于 Python 2:

    >>> variables= {}
    >>> execfile( "someFile.py", variables )
    >>> print variables # globals from the someFile module

关于python - 如何在 Python 解释器中执行文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1027714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com