0

I have a python script "program.py" that takes 2 command line arguments.

When I want to run this program on command line, I should enter:

./python myprogram.py arg1 arg2


However, I want to run my script without the "python" and ".py"

In other words, I want to do this:

./myprogram arg1 arg2


I've written a shell script "myprogram.sh":

#!/bin/bash
python myprogram.py


But I still have to run this by typing

./sh myprogram.sh arg1 arg2

which is still not what I want

Is there a way to achieve this?

Thanks

1

2 Answers 2

3

As the first line of myprogram.py, put the following:

#!/usr/bin/env python

Then, at the command line, in the same directory as myprogram.py, enter the following commands:

mv myprogram.py myprogram
chmod +x myprogram

And you're all set!

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

Comments

1

Put a shebang at the beginning of your script, something like

#! /usr/bin/env python

The file extension is of no matter whatsoever. So just name your file "program" instead of "program.py".

Then give yourself the right to execute it

chmod +x myprogram

Or make it executable for everybody:

chmod a+x myprogram

Then call it from the shell

./myprogram arg1 arg2

Here a full example:

$echo "#! /usr/bin/env python
> print('Here be dragons.')" > program
$chmod +x program
$./program
Here be dragons.
$

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.