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?

3 Answers3

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].

edwinksl
  • 24,109
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).

ubfan1
  • 19,049
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!

muru
  • 207,228
Eduardo Cola
  • 5,876
  • 3
  • 20
  • 33