簡體   English   中英

如何從 IDLE 交互式 shell 運行 python 腳本?

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

如何從 IDLE 交互式 shell 中運行 python 腳本?

以下拋出錯誤:

>>> python helloworld.py
SyntaxError: invalid syntax

蟒蛇3

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(filename).read())

IDLE shell 窗口與終端 shell 不同(例如運行shbash )。 相反,它就像在 Python 交互式解釋器 ( python -i ) 中一樣。 在 IDLE 中運行腳本的最簡單方法是使用File菜單中的Open命令(這可能會因您運行的平台而有所不同)將腳本文件加載到 IDLE 編輯器窗口中,然后使用Run -> Run Module命令(快捷鍵 F5)。

試試這個

import os
import subprocess

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

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

最簡單的方法

python -i helloworld.py  #Python 2

python3 -i helloworld.py #Python 3

execFile('helloworld.py')為我完成了這項工作。 需要注意的是,如果 .py 文件本身不在 Python 文件夾中,則輸入它的完整目錄名稱(至少在 Windows 上是這種情況)

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

例如:

import subprocess

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

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

在 python 控制台中,可以嘗試以下兩種方法。

在同一個工作目錄下,

1. >>導入helloworld

# 如果你有一個變量x ,你可以在 IDLE 中打印它。

>> helloworld.x

# 如果你有一個函數func ,你也可以這樣調用它。

>> helloworld.func()

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

在 Python 3 中,沒有execFile 可以使用exec內置函數,例如:

import helloworld
exec('helloworld')

在 IDLE 中,以下工作:-

 import helloworld

我不太了解它為什么起作用,但它確實..

要在 Python shell(例如 Idle)或 Django shell 中運行 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文檔。

我對此進行了測試,結果有點效果:

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

你可以通過兩種方式做到這一點

  • import file_name

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

但請確保該文件應存儲在您的程序運行的位置

在 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

還有另一種選擇(適用於 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