问题描述
我是Python&的新手。 Eclipse,并且在理解如何将命令行参数传递给在Eclipse(Pydev)中运行的脚本时遇到一些困难。
I am new to Python & Eclipse, and having some difficulties understanding how to pass command line argument to script running within Eclipse(Pydev).
说明了如何将命令行参数传递给python脚本。
The following link explains how to pass command line argument to python script.
传递命令行参数到模块 argecho.py
(来自上面链接的代码),
To pass command line argument to module argecho.py
(code from link above),
#argecho.py
import sys
for arg in sys.argv: 1
print arg
我需要输入python控制台
I would need to type into python console
[you@localhost py]$ python argecho.py
argecho.py
或
[you@localhost py]$ python argecho.py abc def
argecho.py
abc
def
我如何在Eclipse(Pydev)中将相同的参数传递给Python脚本?
How would I pass same arguments to Python script within Eclipse(Pydev) ???
谢谢!
推荐答案
如果您希望程序以交互方式询问参数,则它们不再是 commandline 参数,例如。但是,您可以执行以下操作(仅用于调试!),这将允许您以交互方式输入程序将在命令行参数中看到的值。
If you want your program to ask for arguments interactively, then they cease to be commandline arguments, 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
请注意,安德鲁的处事方式要好得多。另外,如果您使用的是Python 3. *,则应为 input
而不是 raw_input
,
Note that Andrew's way of doing things is much better. Also, if you are using python 3.*, it should be input
instead of raw_input
,
这篇关于在Eclipse(Pydev)中将命令行参数传递给Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!