Run the .py file to the specified line number.

In linux input terminal

python script.py 

It will run run script.py and exit the python console, but what if I just want to run the script part and leave the console open? For example, run script.py before line 15 and leave the console open for future scripts. How can I do it?

Let's say this is possible, and then the console is still open and script.py runs up to line 15, can I then from inside the fragments of the console call line from other py files?

... sort of

 python script.py 15 #(opens script and runs lines 1-15 and leaves console open) 

Then, opening the console, I would like to run lines 25-42 from anotherscript.py

 >15 lines of python code run from script.py > run('anotherscript.py', lines = 25-42) > print "I'm so happy the console is still open so I can script some more") I'm so happy the console is still open so I can script some more > 
+6
source share
4 answers

A better option would be pdb , a Python debugger . You can run the script under pdb , set a breakpoint on line 15, and then run the script.

 python -m pdb script.py b 15 # <-- Set breakpoint on line 15 c # "continue" -> run your program # will break on line 15 

Then you can check your variables and call functions. Starting with Python 3.2, you can also use the interact command inside pdb to get a regular Python shell at the current execution point!

If this matches your score and you also like IPython, you can check IPdb , which is slightly better than regular pdb, and throw you into IPython Shell with interact .

+14
source

if you want to run script.py from line a to line b , just use this bash snippet:

 cat script.py|head -{a+b}|tail -{ba}|python -i 

replace {a+b} and {ba} with your values

+1
source

You can use the python -i option to leave the console open at the end of the script.

It allows your script to work until it exits, and you can then look at the variables, call any function and any Python code, including importing and using other modules.

Of course, your script should exit first, either at the end, or if your goal is to debug this part of the script, you can add a call to sys.exit () or os._exit () where you want it to stop (for example, your line fifteen).

For instance:

 import os print "Script starting" a=1 def f(x): return x print "Exiting on line 8" os._exit(0) # to avoid the standard SystemExit exception print "Code continuing" 

Usage example:

 python -i test_exit.py Scrit starting Exiting on line 8 >>> print a 1 >>> f(4) 4 >>> 
+1
source

You cannot do this directly, but you can do something similar from within the Python console (or IDLE) using exec :

  • just open your favorite Python console.
  • Loading strings into a string and executing them:

     script = 'script.py' txt = '' with open(script) as sc: for i, line in enumerate(sc): if i >= begline and i<= endline: txt = txt + line exec(txt) 

You can even write your own partial runner script based on this code ...

EDIT

I have to admit that only one answer really deserves to be leveled. This is technically correct and probably the one that most closely matches what you requested. But I should have warned you that this is a bad assessment. Relying on line numbers to download fragments of source files is error prone, and you should avoid it if you really don't know what you are doing and why you are doing it. The Python debugger at least allows you to control which lines you are going to execute.

If you really need to use this solution, be sure to always print and double-check the lines you are about to execute. IMHO is always easier and safer to copy and project strings in an IDE, such as IDLE, which is packaged in any standard Python installation.

-2
source

Source: https://habr.com/ru/post/986062/


All Articles