Sunday, June 5, 2016

Running python commands from inside a bash shell

Hello all,

In this post I am going to show you how you can run python commands from inside a shell. There are already a few ways of doing so -
  • Write your python code in a .py file and run "python yourfile.py"
  • Open up IDLE shell by just typing "python" and writing your command in that shell.
There is also another option of python command -

$ python -c 'your_command_here' 

which will run the python command you specify in quotes. 

However, the problem with above mentioned methods is, they are time consuming. If you want to quickly perform a list comprehension or anything equivalent in python, you have to either write a script or manually open a python interactive terminal and write your command there. 

I am going to show you a little tweak you can do in your shell to execute python code (or parts of it) directly from command line. It works as below -
$ p '<your_python_code_here>'
You type "p" followed by the python statement you want to execute in quotes. For example,
Vipuls-MacBook-Pro: ~ $p "[x**2 for x in range(1,11)]" 
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Follow the steps below to add this tweak in your shell.
  1. Open a terminal and go to your home directory.
  2. Create a directory called bin (~/bin) and inside this directory, create a script called "p" (or you can give it any name you like)
  3. Copy the following snippet in this script:
    #!/bin/bash 
    python -c "print $1"
  4. Pretty straightforward. Give execute permissions to this script: 
              $ chmod +x ~/bin/p
  5. Now you need to add this little script to your PATH variable such that shell can find it. To make your shell be able to find this little command, edit the bashrc file (~/.bashrc) to add the following line :
    export PATH=$PATH:~/bin
  6. On some distributions, the name of .bashrc file might be different. (for example, on my Mac OS X, I had to edit .bash_profile file for these changes to take effect.)
  7. After you have added above entry, you may need to restart the terminal.
  8. Now that we have added this little tweak, type the following command to test it.
         $p 2+3
This should show you the result i.e. 5. To run regular python commands, you need to enclose them in double quotes to prevent the shell from interpreting them.
$p "<your_python_command_here>"
Now you can try putting advanced python commands in there. 

This little script "p" that we wrote doesn't actually support advanced python features which python interactive shell offers. You can modify this script to the following :
#!/bin/bash 
echo "import os; 
os.system(\"clear\") 
$1" > tempfile 
python -i < tempfile
This is a little more sophisticated version of our script. It starts the python interactive shell, clears the screen (such that the initial version information that gets printed is wiped out), executes the command we passed in this interactive shell and exits. 

This can be useful in that you can pass shell environment variables to python, manipulate them in python, and get the result back. For example,

Vipuls-MacBook-Pro: ~ $mongodbpath=/Users/vipulchaskar/mongodb/mongodb-osx-x86_64-3.2.1/bin
Vipuls-MacBook-Pro: ~ $p "'$mongodbpath'.split('/')"
['', 'Users', 'vipulchaskar', 'mongodb', 'mongodb-osx-x86_64-3.2.1', 'bin']
Vipuls-MacBook-Pro: ~ $

I honestly don't have further idea about how this can be more useful or how it can be extended further :P If you can think of any improvements for this, please leave a comment below.

Questions/comments/suggestions are welcome :)