rahul
rahul

Reputation: 366

Running Bash scripts in iPython

I wrote a bash script, run.sh which has a python command with multiple options -

python train.py --lr 0.01 \
            --momentum 0.5 \
            --num_hidden 3 \
            --sizes 100,100,100 \
            --activation sigmoid \
            --loss sq \
            --opt adam \
            --batch_size 20 \
            --anneal true

I tried running this command in iPython -

!./run.sh

However, in iPython I'm not able to access the variables of the python script train.py. Is there some way to run the bash script in iPython so that I can access the variables? I don't want to copy paste the above command from the bash script each and every time.

I'm currently using iPython 5.1.0 on macOS Sierra.

Upvotes: 0

Views: 1275

Answers (1)

mfardal
mfardal

Reputation: 176

The python process that runs your script train.py and the python process you're using at the ipython command line are two separate processes. It makes sense that one doesn't know about the variables of the other. There is probably some fancy way to connect the two but I suspect from the way you described the problem that it's not worth the work.

Here's an easier way to get access: you could replace python train.py in your script with python -i train.py. This way you will go into interactive mode in the process that runs your script after it is done, and anything defined at the main level will be accessible. You could insert a call to pdb.set_trace() in your script to stop at an arbitrary point.

Upvotes: 1

Related Questions