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

user2492270
  • 2,215
  • 6
  • 40
  • 56
  • did you make it executable? http://effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm – dm03514 Dec 11 '13 at 00:53

2 Answers2

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!

MattDMo
  • 100,794
  • 21
  • 241
  • 231
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.
$
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87