Published on

Execute Python from Command Line

Authors
  • avatar
    Name
    Tinker Assist
    Twitter

Command Line

When I refer to the "command line", I am talking about a command line interface that allows you to send textual commands for your computer to interpret and execute. In windows, the default command line interface is Command Prompt. In MacOS, the default is Terminal. In order to execute python without calling an interpreter, we are going to have to install Git Bash and leverage some of it's unique characteristics

Git Bash

Git Bash is a Unix-style terminal that comes with Git for Windows download. It's likely that other terminals work fine for what we are trying to do, but in particular we are going to be leveraging ....

Xonsh

Xonsh is a command prompt built on top of python that, at first glace, looks and feels like your typical unix command prompt, but also recognizes and executes python code.

Users can install xonsh as they would install any python library - via pip on the command line. Per the Xonsh website, users can install with the following command

python -m pip install 'xonsh[full]'

Run as Default Command Prompt

Calling 'xonsh' every time you open command prompt or terminal would be a bit annoying, this is where Git Bash comes in handy.

With Git Bash, we can modify the default startup file (git-prompt.sh) to run xonsh every time we open the application.

By default, git-prompt.sh should live in our Program Files (For me it was found in C:\Program Files\Git\etc\profile.d).

We don't want to disturb what typically goes on in the git-prompt.sh file as we want the application startup to go as normal, so we will just append a line to the end of this file with "xonsh" to ensure it is the last command to be executed on startup.

By default, the git bash window should look something like this in Windows

Clean, default git bash terminal

If you run xonsh, the command prompt style should change marginally to look like this

bash terminal after running xonsh

Now, if we append "xonsh" to the bottom of our git-prompt.sh file, we can open git bash and it should look like this

git bash terminal default after adding xonsh to

I pinned Git Bash to my task bar long ago for using xonsh, and I hope this drives you to do the same!