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

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

如何从IDLE交互式外壳程序中运行python脚本?

以下引发错误:

>>> python helloworld.py
SyntaxError: invalid syntax

How do I run a python script from within the IDLE interactive shell?

The following throws an error:

>>> python helloworld.py
SyntaxError: invalid syntax

回答 0

Python3

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

如果您的文件不在同一目录中:

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

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


在不推荐使用的Python版本中

Python2 内置函数: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])

Python3:

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.


In deprecated Python versions

Python2 Built-in function: 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

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:

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

回答 1

您可以在python3中使用它:

exec(open(filename).read())

You can use this in python3:

exec(open(filename).read())

回答 2

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

The IDLE shell window is not the same as a terminal shell (e.g. running sh or bash). Rather, it is just like being in the Python interactive interpreter (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).


回答 3

试试这个

import os
import subprocess

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

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

Try this

import os
import subprocess

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

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

回答 4

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

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

execFile('helloworld.py') does the job for me. 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)

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


回答 5

最简单的方法

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

EASIEST WAY

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

回答 6

例如:

import subprocess

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

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

For example:

import subprocess

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

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

回答 7

在Python 3中,没有execFile。一个可以使用exec内置函数,例如:

import helloworld
exec('helloworld')

In Python 3, there is no execFile. One can use exec built-in function, for instance:

import helloworld
exec('helloworld')

回答 8

在IDLE中,以下工作:

import helloworld

我对它为什么起作用并不十分了解,但是它确实起作用。

In IDLE, the following works :-

import helloworld

I don’t know much about why it works, but it does..


回答 9

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

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

我正在使用Python 3.4。有关详细信息,请参见compileexec文档。

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. Exec() executes a code object argument. A code object in Python is simply compiled Python code. So you must first compile your script file and then execute it using 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. See the compile and exec docs for detailed info.


回答 10

我对此进行了测试,并且可以解决:

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

I tested this and it kinda works out :

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

回答 11

你可以通过两种方式做到

  • import file_name

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

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

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


回答 12

在Windows环境中,您可以使用以下语法在Python3 Shell命令行上执行py文件:

exec(open(’file_name的绝对路径’).read())

下面说明了如何从python shell命令行执行简单的helloworld.py文件

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

文件内容:print(“ hello world”)

我们可以在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

On Windows environment, you can execute py file on Python3 shell command line with the following syntax:

exec(open(‘absolute path to file_name’).read())

Below explains how to execute a simple helloworld.py file from python shell command line

File Location: C:/Users/testuser/testfolder/helloworld.py

File Content: print(“hello world”)

We can execute this file on Python3.7 Shell as below:

>>> 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

回答 13

还有另一种选择(适用于Windows)-

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

There is one more alternative (for windows) –

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

回答 14

在python控制台中,可以尝试以下2种方法。

在同一个工作目录下

1. >>导入helloworld

#如果您有变量x,则可以在IDLE中将其打印出来。

>> helloworld.x

#如果您具有函数func,则也可以这样调用它。

>> helloworld.func()

2. >> runfile(“ ./ helloworld.py”)

In a python console, one can try the following 2 ways.

under the same work directory,

1. >> import helloworld

# if you have a variable x, you can print it in the IDLE.

>> helloworld.x

# if you have a function func, you can also call it like this.

>> helloworld.func()

2. >> runfile(“./helloworld.py”)