0

I am executing python scripts using cmd and have challenges in using other interfaces, so would be executing on CMD only. The python script has 113 lines of code. I want to run and test some selected subsetted line of codes before executing the complete script, without making new python scripts but from the parent script.

From example below (has 28 lines):

To run the parent script we say in cmd:

C:\Users\X> python myMasterDummyScript.py

Can we run just between lines 1 - 16

Dummy Example:

import numpy as np
from six.moves import range
from six.moves import cPickle as pickle


pickle_file = "C:\\A.pickle"

with open(pickle_file, 'rb') as f:
    data = pickle.load(f, encoding ='latin1')

train_dataset = data['train_dataset']
test_dataset = data['test_dataset']
valid_dataset = data['valid_dataset']
train_labels = data['train_labels']
test_labels = data['test_labels']
valid_labels = data['valid_labels']

a = 28
b = 1

def reformat(dataset, labels):
    dataset = dataset.reshape(-1, a, a, b).astype(np.float32)
    labels = (np.arange(10)==labels[:,None]).astype(np.float32)
    return dataset, labels

train_dataset, train_labels = reformat(train_dataset, train_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
Anurag H
  • 909
  • 11
  • 28
  • Possible duplicate of [Pycharm: run only part of my Python file](https://stackoverflow.com/questions/23441657/pycharm-run-only-part-of-my-python-file) – Sum of N upto Infinity Feb 22 '18 at 06:57
  • Hi Nish, thanks for the link, it would be particularly useful if you can help me w.r.t cmd, as shown above – Anurag H Feb 22 '18 at 07:00

4 Answers4

0

Open the parent script in an interpreter like PyCharm and select the lines you want to execute & then right click -> Execute selection in console.

0

It would theoretically be possible with a bit of work, however please note that this is not how scripts work in general. Instead, you should consider grouping coherent routine sequences into named functions, and call them from command line.

Among other issues, you'll have to modify all calling code to your script every time you shift the line numbers, you'll have to repeat any imports any subsection would potentially need and it's generally not a good idea. I am still going to address it after I make a case for refactoring though...

Refactoring the script and calling specific functions

Consider this answer to Python: Run function from the command line

Your python script:

import numpy as np
from six.moves import range
from six.moves import cPickle as pickle

def load_data()
    pickle_file = "C:\\A.pickle"

    with open(pickle_file, 'rb') as f:
        data = pickle.load(f, encoding ='latin1')

    train_dataset = data['train_dataset']
    test_dataset = data['test_dataset']
    valid_dataset = data['valid_dataset']
    train_labels = data['train_labels']
    test_labels = data['test_labels']
    valid_labels = data['valid_labels']

def main():
    a = 28
    b = 1

    def reformat(dataset, labels):
        dataset = dataset.reshape(-1, a, a, b).astype(np.float32)
        labels = (np.arange(10)==labels[:,None]).astype(np.float32)
        return dataset, labels

    train_dataset, train_labels = reformat(train_dataset, train_labels)
    test_dataset, test_labels = reformat(test_dataset, test_labels)
    valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)

Your cmd code would look like this:

REM
REM any logic to identify which function to call
REM

python -c "import myMasterDummyScript; myMasterDummyScript.load_data()"

It also enables you to pass arguments from cmd into the function call.

Now if you're really adamant about running an arbitrary subset of lines from an overall python script...

How to run specific lines from a script in cmd

cmd to read those lines out of the original script and write them to a temporary script Look at a proposed answer for batch script - read line by line. Adapting it slightly without so much of error management (which would significantly bloat this answer):

@echo off
@enabledelayedexpansion

SET startline=$1
SET endline=$2
SET originalscript=$3

SET tempscript=tempscript.py
SET line=1

REM erase tempscript
echo. > %tempscript%

for /f "tokens=*" %%a in (%originalscript%) do (
  if %line% GEQ %startline% (
      if %line% LEQ %endline% (
          echo %%a >> %tempscript%
      )
  )
  set /a line+=1
)

python %tempscript%
pause

You'd call it like this:

C:\> runlines.cmd 1 16 myMasterDummyScript.py
korrigan
  • 372
  • 5
  • 16
0

You could use the command line debugger pdb. As an example, given the following script:

print('1')
print('2')
print('3')
print('4')
print('5')
print('6')
print('7')
print('8')
print('9')
print('10')
print('11')
print('12')
print('13')
print('14')
print('15')

Here's a debug session that runs only lines 5-9 by jumping to line 5, setting a breakpoint at line 10, gives a listing to see the current line to be executed and breakpoints, and continuing execution. Type help to see all the commands available.

C:\>py -m pdb test.py
> c:\test.py(1)<module>()
-> print('1')
(Pdb) jump 5
> c:\test.py(5)<module>()
-> print('5')
(Pdb) b 10
Breakpoint 1 at c:\test.py:10
(Pdb) longlist
  1     print('1')
  2     print('2')
  3     print('3')
  4     print('4')
  5  -> print('5')
  6     print('6')
  7     print('7')
  8     print('8')
  9     print('9')
 10 B   print('10')
 11     print('11')
 12     print('12')
 13     print('13')
 14     print('14')
 15     print('15')
(Pdb) cont
5
6
7
8
9
> c:\test.py(10)<module>()
-> print('10')
(Pdb)
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

Option 1 - You can use a debugger to know everything in any moment of the code execution. (python -m pdb myscript.py debug your code too)

Option 2 - You can create a main file and a sub-files with your pieces of scripts and import in the main script and execute the main file or any separated file to test

Option 3 - You can use arguments (Using the argparse for example)

I've not more options at the moment