13

Question: In command line, how do I call a python script without having to type python in front of the script's name? Is this even possible?


Info:

I wrote a handy script for accessing sqlite databases from command line, but I kind of don't like having to type "python SQLsap args" and would rather just type "SQLsap args". I don't know if this is even possible, but it would be good to know if it is. For more than just this program.

Piotr Dobrogost
  • 41,292
  • 40
  • 236
  • 366
Narcolapser
  • 5,895
  • 15
  • 46
  • 56
  • Which operating system are you using? – Tim McNamara Aug 03 '10 at 20:35
  • Well. for this I'm basically only using unix. it would be nice to achieve the same thing in windows, but it would also be nice to have world peace. Michael Dillon gave the best possible response for getting it to run in windows. efritz's answer i got working just fine. I put it in /usr/bin aswell, so now i can use it from anywhere by just typing SQLsap. rather nice. – Narcolapser Aug 05 '10 at 13:32

6 Answers6

33

You can prepend a shebang on the first line of the script:

#!/usr/bin/env python

This will tell your current shell which command to feed the script into.

efritz
  • 5,125
  • 4
  • 24
  • 33
  • 14
    Also, remember to mark your file as executable. (`chmod +x SQLsap` on POSIX OSs.) – Mike DeSimone Aug 03 '10 at 20:11
  • ah, so that's what that does. I've seen that before but never known. Thanks! – Narcolapser Aug 03 '10 at 20:39
  • 1
    Also, I'd like to note that /usr/bin/env is itself a program. It is used to determine *where a specific executable is. If you know where your python interpreter is (/usr/bin/python, for example), you could simply substitute the shebang to read: #! /usr/bin/python. This goes as well for any executable command - perl, python, bash, etc. – efritz Aug 30 '10 at 01:23
  • What typically happens when a python script, even with `#!/usr/bin/env python` and executable permissions, fail? From the command line, I still have to supply `python` at the beginning of the command line... – Thomas Matthew Sep 30 '15 at 02:21
5

You want a shebang. #!/path/to/python. Put that on the first line of your python script. The #! is actually a magic number that tells the operating system to interpret the file as a script for the program named. You can supply /usr/bin/python or, to be more portable, /usr/bin/env python which calls the /usr/bin/env program and tells it you want the system's installed Python interpreter.

You'll also have to put your script in your path, unless you're okay with typing ./SQLsap args.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
4

Assuming this is on a unix system, you can add a "shebang" on the top of the file like this:
#!/usr/bin/env python

And then set the executable flag like this:
chmod +x SQLsap

baloo
  • 7,635
  • 4
  • 27
  • 35
3

Another way to do this would be to package your python script.

Then all you would have to do is run $ pip install package-name and simply running $ package-name from any directory would execute your python script.

I would recommend this method since it enables you to quickly share/install/use/remove your python script.

I wrote a simple script QuickPackage that can instantly create and upload python package for your awesome script by just entering the path of your python script.

Example:

$ pip install quickpackage

Usage :

Usage Simply run quickpackage and enter the path of your python script.

Example:

❯ quickpackage                                                    
Enter name: quickpackage
Enter version: 1.1
Enter description: Instantly create and upload python package for your script
Enter github url: https://github.com/yask123/quick-package
enter author: Yask Srivastava
Enter email: yask123@gmail.com
Path of python script file: run.py
running register
....
running upload
Submitting dist/quickpackage-1.1.tar.gz to https://pypi.python.org/pypi
Server response (200): OK

Now simply execute your script by running$ quickpackage (In this case)

yask
  • 3,918
  • 4
  • 20
  • 29
2

On Windows or DOS you can come close by putting your python code into a .BAT file like this.

rem = ''' this line interpreted by BAT and PYTHON
@REM BAT-ONLY code begins now inside PYTHON comment
@echo off
python "%~dpnx0" %*
goto :eof
@REM CMD-ONLY code ends
'''

import sys
print sys.path

Unfortunately, I can't get rid of the first line message on the output, but you could always change the text after ''' to be something like the application's name and people wouldn't notice.

Ideally, you wouldn't put lots of Python code in the .BAT file but would import a .py file and then run its .__main__ method.

Michael Dillon
  • 31,973
  • 6
  • 70
  • 106
1

In unix, you use a shebang line at the start of your script:

#!/usr/bin/env python

make the file executable:

chmod +x arbitraryname

and put it in a directory on your PATH (can be a symlink):

cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname

In windows, files like *.py has been set to be open with python.exe by default. (If not, you can set manually.) So you can run *.py files in console directly.

Note that:

In windows, text new line code is "\r\n", but in unix, it is "\n". If your python script is windows format, then executing in unix will report "No such file or directory" error. To repair this problem, just replace all "\r\n" with "\n" in unix will be ok.

cfh008
  • 436
  • 5
  • 8