如何在终端中执行一行 python 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24452749/
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
How to execute a single line of a python script in Terminal?
提问by macnsteeeze
I have the current simple script saved as ex1.py in the Sublime Text 2 IDE.
我在 Sublime Text 2 IDE 中将当前的简单脚本保存为 ex1.py。
print "Hello world!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
I would like to execute a single line from this script in Terminal, but haven't been able to figure out how.
我想在终端中从这个脚本中执行一行,但一直无法弄清楚如何执行。
The script executes all seven lines. Is there a way to specify, for example, that I just want to execute line 3?
该脚本执行所有七行。例如,有没有办法指定我只想执行第 3 行?
回答by damienfrancois
As @Wooble says, this is an odd requirement, but anyway, here is a solution in a Bash session:
正如@Wooble 所说,这是一个奇怪的要求,但无论如何,这是 Bash 会话中的解决方案:
Use awk to extract the line you want (e.g. line 2):
使用 awk 提取您想要的行(例如第 2 行):
$ awk 'NR==2' ex1.py
print "Hello Again"
Then feed it to the Python interpreter through stdin
.
然后通过stdin
.
$ awk 'NR==2' ex1.py | python
Hello Again
You can also specify a range
您还可以指定一个范围
$ awk 'NR>=2 && NR<=4' ex1.py | python
Hello Again
I like typing this.
This is fun.
Edit: note that in this case, the equivalent sed
command requires fewer keystrokes
编辑:请注意,在这种情况下,等效sed
命令需要更少的击键
$ sed -n '2,4 p' ex1.py | python
Hello Again
I like typing this.
This is fun.
回答by jmunsch
You could use (pdb
):
您可以使用 ( pdb
):
import pdb;pdb.set_trace()
print "Hello world!"
print "Hello Again"
print "I like typing this."
print "This is fun."
print 'Yay! Printing.'
print "I'd much rather you 'not'."
print 'I "said" do not touch this.'
You could then step through:
然后,您可以逐步完成:
step
step
or jump to a single line (line 3):
或跳到单行(第 3 行):
j 3
j 3
Or if you want to run a single command from terminal: python -c "print('hello there')"
或者,如果您想从终端运行单个命令: python -c "print('hello there')"
回答by Shadow9043
Another way to do this would be to pass what line you want to your script and have your script decide what line to show.
另一种方法是将您想要的行传递给您的脚本,并让您的脚本决定要显示的行。
Have a look at Python argparsemodule.
看看 Python argparse模块。
Use it to create command line parser then use if
statements to drive what gets shown.
使用它来创建命令行解析器,然后使用if
语句来驱动所显示的内容。
e.g.
例如
if arg_parse_result == 3:
print "I like typing this."
You could then on command line do something like:
然后,您可以在命令行上执行以下操作:
C:\>python ex1.py --line 3
回答by Movsar Bekaev
It's an assignment from course Python The Hard Way by Zed A. Shawand it's not for professionals with doing weird things like extracting text and feeding it through streams... Anyway, in this assignment author wanted to make newbies acquainted with the way commentaries work in programming languages, as you can see from the original assignment:
这是Zed A. Shaw 的 Python The Hard Way课程中的一项作业,它不适合从事诸如提取文本和通过流馈送之类的奇怪事情的专业人士……无论如何,在此作业中,作者想让新手熟悉评论的工作方式在编程语言中,正如您从原始作业中看到的那样:
The Study Drills contain things you should try to do. If you can't, skip it and come back later.
For this exercise, try these things:
1. Make your script print another line.
2. Make your script print only one of the lines.
3. Put a # (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.
Here you can see how author intends to make newbie first frustrated by the hard question (2), but after going to the next exercise (3) make him realize that he can use # for the one which he got frustrated about.
在这里,您可以看到作者打算如何让新手首先因难题 (2) 而感到沮丧,但在进行下一个练习 (3) 之后,让他意识到他可以使用 # 来表示他感到沮丧的问题。
So here's the right answer specifically to this question: use # to comment all lines but one.
所以这里是这个问题的正确答案:使用 # 注释除一行之外的所有行。
回答by anand_mars
I am reading the book "learn python the hard way", and the solution is using the mesh symbol to prevent the terminal from running certain lines.
我正在阅读“以艰难的方式学习python”一书,解决方案是使用网格符号来防止终端运行某些行。
回答by dipanjan sanyal
Why don't you check out Rodeo... I have worked in R studio and now learning python, Rodeo feels similar
你为什么不看看Rodeo...我在R studio工作,现在正在学习python,Rodeo感觉很像