run python script directly from command line
Asked Answered
I

3

12
#!/usr/bin/env python

I put that at the top of a script. I've seen that should make the script runnable from the command line without the need for python programname.py. Unless I'm misunderstanding I should be able to use programname.py as long as I have the above line at the top of the script. Is this correct?

It isn't working for me I just get an error indicating that I would have to use python at the beginning of the 'call'.

Irina answered 1/12, 2013 at 22:36 Comment(0)
I
28

Universal running of Python scripts

You can pretty much universally run without the shebang (#!) with

python myscript.py

Or nearly equivalently (it places the current directory on your path and executes the module named myscript) (preferably do this!):

python -m myscript

from the command line, as long as you have Python installed and on your path environment variable (i.e. set to run with python, which, if installed, would typically be the case).

Shebangs (#!) are a Unix thing.

The shebang, as you're using it, is typically for running on a Unix platform (typically Apple or Linux). Windows would typically require cygwin to use the shebang.

You can usually default to whatever python is available on your system path with:

#!/usr/bin/env python

Assuming you're on a Unix, you might try other locations for your python setup, like:

#!/usr/bin/python

Muddling through

You can see what python you're currently using by using the unix which command, so if you want to see where your python is coming from, use this command:

which python

or on Windows (cygwin probably can run the shebang):

where python

On Linux/Unix, you'll need execution perms to run the file as well, in that manner. Use chmod

chmod +x myscript.py

(chmod also may apply to Cygwin in Windows)

If you're not running as root, you may require sudo, and that would be

sudo chmod +x myscript.py

And then attempt to run (within the same directory) with

./myscript.py 
Immediacy answered 1/12, 2013 at 22:53 Comment(1)
Python shebangs are no longer a Unix thing. Python has implemented "virtual shebangs", which work validly on both Unix and Windows. See docs.python.org/3/using/windows.html#shebang-linesHermaherman
B
6

make the file executable

sudo chmod +x /path/to/file.py

and then from the same directory as file.py:

./file.py
Belfast answered 1/12, 2013 at 23:6 Comment(0)
H
0

I had the same: I could not run any executable files. Reason was that I had put a partition (my HDD) in /etc/fstab without exec permissions. The solution was to change:

UUID=asdfasfasdfa /mnt/fee auto rw 0 1

to

UUID=asdfasfasdfa /mnt/fee auto defaults 0 1

because exec is included in defaults = rw, suid, dev, exec, auto, nouser, and async. On NTFS or other, you cannot change the permissions on files or folders in a partition, but that is not the OP's question, because permissions were set properly on the file.

Hyposensitize answered 29/6, 2023 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.