将命令行参数传递给 Eclipse(Pydev) 中的 Python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4355721/
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
Passing command Line argument to Python script within Eclipse(Pydev)
提问by newprint
I am new to Python & Eclipse, and having some difficulties understanding how to pass command line argument to script running within Eclipse(Pydev).
我是 Python 和 Eclipse 的新手,在理解如何将命令行参数传递给在 Eclipse(Pydev) 中运行的脚本时遇到了一些困难。
The following linkexplains how to pass command line argument to python script.
以下链接解释了如何将命令行参数传递给 python 脚本。
To pass command line argument to module argecho.py(code from link above),
将命令行参数传递给模块argecho.py(来自上面链接的代码),
#argecho.py
import sys
for arg in sys.argv: 1
print arg
I would need to type into python console
我需要在 python 控制台中输入
[you@localhost py]$ python argecho.py
argecho.py
or
或者
[you@localhost py]$ python argecho.py abc def
argecho.py
abc
def
How would I pass same arguments to Python script within Eclipse(Pydev) ???
我如何将相同的参数传递给 Eclipse(Pydev) 中的 Python 脚本???
Thanks !
谢谢 !
采纳答案by Blue Peppers
If you want your program to ask for arguments interactively, then they cease to be commandlinearguments, as such. However you could do it something like this (for debugging only!), which will allow you to interactively enter values that the program will see as command line arguments.
如果您希望您的程序以交互方式请求参数,那么它们就不再是命令行参数。但是,您可以这样做(仅用于调试!),这将允许您以交互方式输入程序将视为命令行参数的值。
import sys
sys.argv = raw_input('Enter command line arguments: ').split()
#Rest of the program here
Note that Andrew's way of doing things is muchbetter. Also, if you are using python 3.*, it should be inputinstead of raw_input,
请注意,安德鲁的做事方式要好得多。另外,如果您使用的是 python 3.*,它应该是input而不是raw_input,
回答by Andrew White
回答by BenH
Select "Properties" -->> "Run/Debug Settings".
选择“属性”-->>“运行/调试设置”。
Select the related file in right panel and then click on "Edit" button. It will open properties of selected file. There's an "Arguments" tab.
在右侧面板中选择相关文件,然后单击“编辑”按钮。它将打开所选文件的属性。有一个“参数”选项卡。
回答by Vineet
What I do is:
我要做的是:
Open the project in debug perspective. In the console, whenever the debugger breaks at breakpoint, you can type python command in the "console" and hit return (or enter). There is no ">>" symbol, so it is hard to discover.
在调试角度打开项目。在控制台中,每当调试器在断点处中断时,您可以在“控制台”中键入 python 命令并按回车(或回车)。没有“>>”符号,所以很难发现。
But I wonder why eclipse doesn't have a python shell :(
但我想知道为什么 eclipse 没有 python shell :(
回答by denis
Years later, and not Eclipse,
but a variant of other answers to run my.py M=11 N=None ...in sh or IPython:
多年后,不是 Eclipse,而是run my.py M=11 N=None ...sh 或 IPython中其他答案的变体:
import sys
# parameters --
M = 10
N = 20
...
# to change these params in sh or ipython, run this.py M=11 N=None ...
for arg in sys.argv[1:]:
exec( arg )
...
myfunc( M, N ... )
See One-line-arg-parse-for-flexible-testing-in-pythonunder gist.github.com/denis-bz.
请参阅gist.github.com/denis-bz下的One-line-arg-parse-for-flexible-testing-in-python。


