0

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.

3
  • Start your cell with %%bash, then you can input shell code. This is called bash cell magic, if you want to google it. Commented Feb 6, 2017 at 5:35
  • @cel: This doesn't seem to help. Have tried it. Commented Feb 6, 2017 at 5:41
  • Instead of the %%bash magic, suggested by @cel , you could try to use the %%python magic and then put your script inside the cell and run it directly with the parameters you want Commented Nov 11, 2020 at 12:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.