0

I have a python script, let's call it my_script.py. The script is supposed to generate simulated responses to an api call performed on the command line.

What I am looking for is some way to have a custom command line command, e.g. run-my-script execute my_script.py while allowing for my_script.py to accept keyword arguments.

It would be great to have a solution that doesn't require external libraries and that allows for keyword arguments to be passed from the command line to that function.

Is there a way to have a custom command on the command line, like run-my-script trigger my_script.py?

Though it would be easiest to just run python my_script.py from the command line, for my use case I need to have the python script triggered just by run-my-script alone.

4
  • 2
    You can rename your script to run-my-script if you already have the shebang line, then copy it to somewhere on your path. Commented Jan 7, 2020 at 0:12
  • 2
    Like a shell script named run-my-script that itself runs python3 my_script.py "$@"? Commented Jan 7, 2020 at 0:18
  • @jarmod That is probably one solution. But it seems like more work than necessary since you can run my_script.py directly without a shell script wrapper. Commented Jan 7, 2020 at 0:48
  • @Code-Apprentice agreed, if the only thing the shell script does is exec the Python script. I figured it might be more complex than that, but maybe it isn’t. Commented Jan 7, 2020 at 0:51

2 Answers 2

2

alias run-my-script="python my_script.py"

Run this command in terminal and run-my-script will execute my_script.py.

You can also add it in ~/.bashrc so you can access it after you reboot.

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

Comments

1

You should first add a shebang line to your Python script:

#!/usr/bin/env python3

Then you can run it directly from the command line as

$ my_script.py

If you want the command to be something else, then just rename your file. If you want to run it from any directory, copy it to a directory that is already in you PATH or add it's parent directory to your PATH.

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.