在 Python shell 中运行程序

发布于 2024-12-04 11:47:03 字数 129 浏览 4 评论 0原文

我有一个演示文件:test.py。 在 Windows 控制台中,我可以使用以下命令运行该文件: C:\>test.py

如何在 Python Shell 中执行该文件?

I have a demo file: test.py.
In the Windows Console I can run the file with: C:\>test.py

How can I execute the file in the Python Shell instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故人爱我别走 2024-12-11 11:47:03

对于 Python 2 使用 execfile

>>> execfile('C:\\test.py')

使用 < a href="https://docs.python.org/3/library/functions.html#exec" rel="noreferrer">exec 用于Python 3

>>> exec(open("C:\\test.py").read())

Use execfile for Python 2:

>>> execfile('C:\\test.py')

Use exec for Python 3

>>> exec(open("C:\\test.py").read())
最笨的告白 2024-12-11 11:47:03

如果您想运行脚本并在提示符处结束(以便您可以检查变量等),请使用:

python -i test.py

这将运行脚本,然后将您带入 Python 解释器。

If you're wanting to run the script and end at a prompt (so you can inspect variables, etc), then use:

python -i test.py

That will run the script and then drop you into a Python interpreter.

无法言说的痛 2024-12-11 11:47:03

这取决于 test.py 中的内容。以下是适当的结构:

# suppose this is your 'test.py' file
def main():
 """This function runs the core of your program"""
 print("running main")

if __name__ == "__main__":
 # if you call this script from the command line (the shell) it will
 # run the 'main' function
 main()

如果保留此结构,您可以在命令行中像这样运行它(假设 $ 是您的命令行提示符):

$ python test.py
$ # it will print "running main"

如果您想从Python shell,然后您只需执行以下操作:

>>> import test
>>> test.main() # this calls the main part of your program

如果您已经在使用 Python,则无需使用 subprocess 模块。相反,尝试以可以从命令行和 Python 解释器运行的方式构建 Python 文件。

It depends on what is in test.py. The following is an appropriate structure:

# suppose this is your 'test.py' file
def main():
 """This function runs the core of your program"""
 print("running main")

if __name__ == "__main__":
 # if you call this script from the command line (the shell) it will
 # run the 'main' function
 main()

If you keep this structure, you can run it like this in the command line (assume that $ is your command-line prompt):

$ python test.py
$ # it will print "running main"

If you want to run it from the Python shell, then you simply do the following:

>>> import test
>>> test.main() # this calls the main part of your program

There is no necessity to use the subprocess module if you are already using Python. Instead, try to structure your Python files in such a way that they can be run both from the command line and the Python interpreter.

如痴如狂 2024-12-11 11:47:03

对于较新版本的 python:

exec(open(filename).read())

For newer version of python:

exec(open(filename).read())
流年里的时光 2024-12-11 11:47:03

如果你想避免每次都编写所有这些,你可以定义一个函数:

def run(filename):
    exec(open(filename).read())

然后调用它

run('filename.py')

If you want to avoid writing all of this everytime, you can define a function :

def run(filename):
    exec(open(filename).read())

and then call it

run('filename.py')
只为一人 2024-12-11 11:47:03

从同一文件夹中,您可以执行以下操作:

import test

From the same folder, you can do:

import test
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文