如何使用变量在 Spyder 控制台中执行 Python 3.3 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18502876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:54:50  来源:igfitidea点击:

how to execute Python 3.3 script in Spyder console with variables?

pythonpython-3.xspyder

提问by uday

how can I execute Python 3.3 script in Spyder console, and that has variables?

如何在 Spyder 控制台中执行 Python 3.3 脚本,并且该脚本具有变量?

My sample code (C:/test/myfile.py) is

我的示例代码 (C:/test/myfile.py) 是

from sys import argv
script, first, second, third = argv
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

I have tried exec(open("C:\test\myfile.py").read()) - and the error I get is "ValueError: need more than 1 value to unpack. I want to supply the variables first = "1st", second = "2nd", third = "3rd". How can I write the exec() so that it can handle the inputs?

我试过 exec(open("C:\test\myfile.py").read()) - 我得到的错误是“ValueError: 需要 1 个以上的值来解包。我想先提供变量 =” 1st", second = "2nd",third = "3rd"。如何编写 exec() 以便它可以处理输入?

I'm using Python 3.3, 64-bit installation, Windows OS, installation: WinPython.

我使用的是 Python 3.3,64 位安装,Windows 操作系统,安装:WinPython。

采纳答案by Carlos Cordoba

You need to go

你需要去

Run > Configuration per file

Run > Configuration per file

(or hit Ctrl+F6) and in the dialog that it appears you need to check

(或点击Ctrl+F6)并在出现的对话框中,您需要检查

Command line options

Command line options

and write (for example) there

并在那里写(例如)

1 2 3

1 2 3

After closing this dialog and hitting F5, you'll see the output you are expecting.

关闭此对话框并点击 后F5,您将看到您期望的输出。

Note: Please remember that these command line options are saved between Spyder restarts as part of the file run config, so if you want to change them, you need to hit Ctrl+F6again.

注意:请记住,这些命令行选项作为文件运行配置的一部分在 Spyder 重新启动之间保存,因此如果要更改它们,则需要Ctrl+F6再次点击。

回答by DieterP

What also works is the IPython console of Spyder is:

同样有效的是 Spyder 的 IPython 控制台是:

In [1]: runfile('C:/yourfolder/myfile.py',args='one two three')

在 [1]: runfile('C:/yourfolder/myfile.py',args='一二三')