简体   繁体   中英

How to execute a single line of a python script in Terminal?

I have the current simple script saved as ex1.py in the Sublime Text 2 IDE.

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?

As @Wooble says, this is an odd requirement, but anyway, here is a solution in a Bash session:

Use awk to extract the line you want (eg line 2):

$ awk 'NR==2' ex1.py 
print "Hello Again"

Then feed it to the Python interpreter through 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 -n '2,4 p' ex1.py  | python
Hello Again
I like typing this.
This is fun.

It's an assignment from course Python The Hard Way by Zed A. Shaw and 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:

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.

So here's the right answer specifically to this question: use # to comment all lines but one.

You could use ( 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

or jump to a single line (line 3):

j 3

Or if you want to run a single command from terminal: python -c "print('hello there')"

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 argparse module.

Use it to create command line parser then use if statements to drive what gets shown.

eg

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

我正在阅读“以艰难的方式学习python”一书,解决方案是使用网格符号来防止终端运行某些行。

你为什么不看看Rodeo...我在R studio工作,现在正在学习python,Rodeo感觉很像

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM