python交互式shell是哪里_如何从IDLE交互式shell运行python脚本?

本文介绍了如何从IDLE交互式shell运行Python脚本,包括使用内置函数`execfile`(已弃用)、`os.popen`、`subprocess`模块等方法,并给出了各种带有参数的示例。
摘要由CSDN通过智能技术生成

如何从IDLE交互式shell运行python脚本?

如何从IDLE交互式shell中运行python脚本?

以下引发错误:

>>> python helloworld.py

SyntaxError: invalid syntax

12个解决方案

100 votes

内置函数:execfile

execfile('helloworld.py')

它通常不能用参数调用。 但这是一个解决方法:

import sys

sys.argv = ['helloworld.py', 'arg'] # argv[0] should still be the script name

execfile('helloworld.py')

自2.6以来不推荐使用:popen

import os

os.popen('python helloworld.py') # Just run the program

os.popen('python helloworld.py').read() # Also gets you the stdout

有参数:

os.popen('python helloworld.py arg').read()

高级用法:子进程

import subprocess

subprocess.call(['python', 'helloworld.py']) # Just run the program

subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout

有参数:

subprocess.call(['python', 'helloworld.py', 'arg'])

阅读文档了解详情:-)

测试了这个基本的helloworld.py:

import sys

if len(sys.argv) > 1:

print(sys.argv[1])

Hugues Fontenelle answered 2019-05-10T15:13:22Z

26 votes

你可以在python3中使用它:

exec(open(filename).read())

FacePalm answered 2019-05-10T15:13:49Z

20 votes

IDLE shell窗口与终端shell不同(例如,运行sh或bash)。 相反,它就像是在Python交互式解释器中(python -i)。 在IDLE中运行脚本的最简单方法是使用File菜单中的Open命令(这可能会有所不同,具体取决于您运行的平台)将脚本文件加载到IDLE编辑器窗口,然后使用Run - > Run Module命令(快捷键F5)。

Ned Deily answered 2019-05-10T15:14:19Z

5 votes

试试这个

import os

import subprocess

DIR = os.path.join('C:\\', 'Users', 'Sergey', 'Desktop', 'helloword.py')

subprocess.call(['python', DIR])

Sergey Nosov answered 2019-05-10T15:14:43Z

4 votes

execFile('C:/helloworld.py')为我工作。 需要注意的是输入.py文件的完整目录名称,如果它不在Python文件夹本身中(至少在Windows上就是这种情况)

例如,execFile('C:/helloworld.py')

optimistic_kid answered 2019-05-10T15:15:19Z

2 votes

例如:

import subprocess

subprocess.call("C:\helloworld.py")

subprocess.call(["python", "-h"])

Sergey Nosov answered 2019-05-10T15:15:41Z

2 votes

最简单的方式

python -i helloworld.py #Python 2

python3 -i helloworld.py #Python 3

Leonard answered 2019-05-10T15:16:10Z

1 votes

在IDLE中,以下作品: -

import helloworld

我不知道它为什么会起作用,但确实如此......

Aditya Dixit answered 2019-05-10T15:16:53Z

1 votes

要在python shell(如Idle)或Django shell中运行python脚本,可以使用exec()函数执行以下操作。 Exec()执行代码对象参数。 Python中的代码对象只是编译的Python代码。 因此,您必须首先编译脚本文件,然后使用exec()执行它。 从你的shell:

>>>file_to_compile = open('/path/to/your/file.py').read()

>>>code_object = compile(file_to_compile, '', 'exec')

>>>exec(code_object)

我正在使用Python 3.4。 有关详细信息,请参阅compile和exec文档。

y2knoproblem answered 2019-05-10T15:17:38Z

0 votes

在Python 3中,没有exec.可以使用exec内置函数,例如:

import helloworld

exec('helloworld')

piogor answered 2019-05-10T15:18:09Z

0 votes

我测试了这个,它有点成功:

exec(open('filename').read()) # Don't forget to put the filename between ' '

Remache Amine answered 2019-05-10T15:18:36Z

0 votes

你可以通过两种方式来做到这一点

exec(open('file_name').read())

exec(open('file_name').read())

但请确保该文件应存储在程序运行的位置

ujjal das answered 2019-05-10T15:19:22Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值