1

I have a Python script which takes command line arguments.

When I want to run the script I have to navigate to its directory and run:

python myscript.py [arguments]

How can I run it like:

myscript [arguments]

Do I have to create a package? If so, how?

0
1

Edit your script so your first line is a shebang pointing to the desired interpreter's executable path.

First, find where the python executable is, with which python.

Then, in your script's first line, add

#!/path/to/python

Then run

mkdir $HOME/bin

And put your script there.

That should do it!

2
  • This worked after I changed the name of 'myscript.py' to just 'myscript'. – Conor McCauley May 22 '16 at 23:22
  • 1
    The best way to do this is #!/usr/bin/env python . this way you can change python version easily and you script can adjust to more system layouts – Amias May 23 '16 at 8:50
3

First, make sure myscript.py is executable by doing chmod +x myscript.py. Second, ensure that the shebang #!/usr/bin/env python appears as the first line of myscript.py. You then execute the script using ./myscript.py [arguments].

2

Add

#!/usr/bin/python

to the first line of your script. This presumes that you have python installed and runnable from /usr/bin, and that the current directory (.) is in your PATH environment variable (not the default,but you can change it in your home .profile file).

Not the answer you're looking for? Browse other questions tagged or ask your own question.