6

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?

6
  • 1
    Thanks, but that executes all seven lines. Is there a way to specify, for example, that I just want to execute line 3? Commented Jun 27, 2014 at 13:15
  • 1
    use conditonal statement and argument to specifu which line to be exuted . first of all please go through first docs of python u can find all docs.python.org/2/tutorial/interpreter.html here Commented Jun 27, 2014 at 13:16
  • 3
    Python doesn't work that way. You could probably hack together something in bash that pulls the third line out and executes it with python -c, but this is a very odd requirement. Commented Jun 27, 2014 at 13:17
  • If you have python installed just go into terminal then either type python and press enter to start the interpreter and paste the line and run it or type python then your line of code Commented Jun 27, 2014 at 13:22
  • The other option is to just break the lines up into several scripts. Then you can choose which script to run. Commented Jun 27, 2014 at 13:36

6 Answers 6

11

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 (e.g. 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.
Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

Having just done this exercise as a complete beginner, I have to agree with this answer.
1

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')"

Comments

0

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.

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

Comments

0

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.

Comments

-1

Why don't you check out Rodeo... I have worked in R studio and now learning python, Rodeo feels similar

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.