Run Python Scripts using AutoHotkey and Pass Arguments

So you have a python script that has some code like:

import argparse

parser = argparse.ArgumentParser(description='For my visitors!')
parser.add_argument("--argument_to_be_passed", required=True, type=str, help="Please pass a value into --argument_to_be_passed")
args = parser.parse_args()

value = args.argument_to_be_passed

Now, if we run this script, this is the output:

Execution without passing arguments

But if we pass arguments manually:

Arguments are getting passed manually and then printed on command line.

So you see, it is working. But this is slow and boring.

What if you wanted to automate running a python script in order to QUICKLY test something and development purpose!

I know from experience the process of ALT+TAB into a CMD and then pressing UP arrow and press enter is a repetitive labour. Lets reward ourselves with Autohotkey’s automation and buy some extra minutes into the already tightened day.


Once you have installed AutoHotkey, it is time write this script:

; Autoexecute
    #SingleInstance force
    run cmd.exe
    WinWait, ahk_exe cmd.exe ; Wait for CMD to start
    Send c:{enter} ; Go to C drive
    Send cd C:\python\blog\{enter} ; go to script's folder
    Send python --argument_to_be_passed CODEONION{enter}

Make sure the path to python script is correct. Run this AutoHotkey script file and then, use CTRL+WINDOWSKEY+SPACE to run the macro. And enjoy the spam show!

See it in action!

Get code from my Github Gist

I really hope this made your day! Please commend and share if oyu are feeling generous. Thanks! and have a nice day!

By codeonion

Codeonion contributes to the honorable mission of “helping people learn and earn online”. Whether your day job is creative or not, life is better when you learn and apply creative skills regularly. To do this is satisfying in its own right and a practical investment in your future.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.