简体   繁体   English

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

[英]How to run a python script from IDLE interactive shell?

How do I run a python script from within the IDLE interactive shell?如何从 IDLE 交互式 shell 中运行 python 脚本?

The following throws an error:以下抛出错误:

>>> python helloworld.py
SyntaxError: invalid syntax

Python3 :蟒蛇3

exec(open('helloworld.py').read())

If your file not in the same dir:如果您的文件不在同一个目录中:

exec(open('./app/filename.py').read())

See https://stackoverflow.com/a/437857/739577 for passing global/local variables.有关传递全局/局部变量的信息,请参阅https://stackoverflow.com/a/437857/739577


In deprecated Python versions在已弃用的 Python 版本中

Python2 Built-in function: execfile Python2内置函数: execfile

execfile('helloworld.py')

It normally cannot be called with arguments.它通常不能用参数调用。 But here's a workaround:但这里有一个解决方法:

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

Deprecated since 2.6: popen自 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

With arguments:有论据:

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

Advance usage: subprocess高级用法:子进程

import subprocess
subprocess.call(['python', 'helloworld.py']) # Just run the program
subprocess.check_output(['python', 'helloworld.py']) # Also gets you the stdout

With arguments:有论据:

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

Read the docs for details :-)阅读文档以获取详细信息:-)


Tested with this basic helloworld.py :用这个基本的helloworld.py测试:

import sys
if len(sys.argv) > 1:
    print(sys.argv[1])

你可以在 python3 中使用它:

exec(open(filename).read())

The IDLE shell window is not the same as a terminal shell (eg running sh or bash ). IDLE shell 窗口与终端 shell 不同(例如运行shbash )。 Rather, it is just like being in the Python interactive interpreter ( python -i ).相反,它就像在 Python 交互式解释器 ( python -i ) 中一样。 The easiest way to run a script in IDLE is to use the Open command from the File menu (this may vary a bit depending on which platform you are running) to load your script file into an IDLE editor window and then use the Run -> Run Module command (shortcut F5).在 IDLE 中运行脚本的最简单方法是使用File菜单中的Open命令(这可能会因您运行的平台而有所不同)将脚本文件加载到 IDLE 编辑器窗口中,然后使用Run -> Run Module命令(快捷键 F5)。

Try this试试这个

import os
import subprocess

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

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

EASIEST WAY最简单的方法

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

execFile('helloworld.py') does the job for me. execFile('helloworld.py')为我完成了这项工作。 A thing to note is to enter the complete directory name of the .py file if it isnt in the Python folder itself (atleast this is the case on Windows)需要注意的是,如果 .py 文件本身不在 Python 文件夹中,则输入它的完整目录名称(至少在 Windows 上是这种情况)

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

For example:例如:

import subprocess

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

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

In a python console, one can try the following 2 ways.在 python 控制台中,可以尝试以下两种方法。

under the same work directory,在同一个工作目录下,

1. >> import helloworld 1. >>导入helloworld

# if you have a variable x , you can print it in the IDLE. # 如果你有一个变量x ,你可以在 IDLE 中打印它。

>> helloworld.x >> helloworld.x

# if you have a function func , you can also call it like this. # 如果你有一个函数func ,你也可以这样调用它。

>> helloworld.func() >> helloworld.func()

2. >> runfile("./helloworld.py") 2. >> 运行文件("./helloworld.py")

In Python 3, there is no execFile .在 Python 3 中,没有execFile One can use exec built-in function, for instance:可以使用exec内置函数,例如:

import helloworld
exec('helloworld')

In IDLE, the following works :-在 IDLE 中,以下工作:-

 import helloworld

I don't know much about why it works, but it does..我不太了解它为什么起作用,但它确实..

To run a python script in a python shell such as Idle or in a Django shell you can do the following using the exec() function.要在 Python shell(例如 Idle)或 Django shell 中运行 Python 脚本,您可以使用 exec() 函数执行以下操作。 Exec() executes a code object argument. Exec() 执行代码对象参数。 A code object in Python is simply compiled Python code. Python 中的代码对象只是编译后的 Python 代码。 So you must first compile your script file and then execute it using exec().所以你必须首先编译你的脚本文件,然后使用 exec() 执行它。 From your shell:从你的外壳:

 >>>file_to_compile = open('/path/to/your/file.py').read() >>>code_object = compile(file_to_compile, '<string>', 'exec') >>>exec(code_object)

I'm using Python 3.4.我正在使用 Python 3.4。 See the compile and exec docs for detailed info.有关详细信息,请参阅compileexec文档。

我对此进行了测试,结果有点效果:

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

you can do it by two ways你可以通过两种方式做到这一点

  • import file_name

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

but make sure that file should be stored where your program is running但请确保该文件应存储在您的程序运行的位置

On Windows environment, you can execute py file on Python3 shell command line with the following syntax:在 Windows 环境下,您可以使用以下语法在 Python3 shell 命令行上执行 py 文件:

exec(open('absolute path to file_name').read()) exec(open('file_name 的绝对路径').read())

Below explains how to execute a simple helloworld.py file from python shell command line下面解释如何从 python shell 命令行执行一个简单的 helloworld.py 文件

File Location: C:/Users/testuser/testfolder/helloworld.py文件位置:C:/Users/testuser/testfolder/helloworld.py

File Content: print("hello world")文件内容:print("hello world")

We can execute this file on Python3.7 Shell as below:我们可以在 Python3.7 Shell 上执行这个文件,如下所示:

>>> import os
>>> abs_path = 'C://Users/testuser/testfolder'
>>> os.chdir(abs_path)
>>> os.getcwd()
'C:\\Users\\testuser\\testfolder'

>>> exec(open("helloworld.py").read())
hello world

>>> exec(open("C:\\Users\\testuser\\testfolder\\helloworld.py").read())
hello world

>>> os.path.abspath("helloworld.py")
'C:\\Users\\testuser\\testfolder\\helloworld.py'
>>> import helloworld
hello world

There is one more alternative (for windows) -还有另一种选择(适用于 Windows)-

    import os
    os.system('py "<path of program with extension>"')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM