Classified
Classified

Reputation: 6060

Different ways to execute python script

I think I already answered my own question in my mind but in case there are other reasons I don't see, I am wondering what's the benefit/difference between running a python script with

python script.py

and using the shebang

#!/usr/local/bin/python

I usually run my scripts as self executing scripts so I use the shebang. I just came across a team member who runs his programs using

python script.py

My question is which way is better or is this a personal preference thing?

If I run with shebang, I can specify which version I want/need to use. The only problem is I need to make sure that version is installed at that location.

If I run it the other way (not sure what to call it, non-shebang way?), I need to make sure the version I want to use is in my path or defined correctly in my path. I could also type in the full path of python but that can get rather tiring if the path is very long.

Are there other benefits/drawbacks that I don't see? Thanks.

Upvotes: 2

Views: 822

Answers (4)

Jason Hu
Jason Hu

Reputation: 6333

In general environment they are the same.

However, shebang gives you an extra benefit that you can replace your executable very easily without changing any other files just by substituting the file. The second(better) implementation can potentially be anything. For a script based environment, I always use shebang, with a fixture of languages.

for the shebang line, the argument that you need to be concerned about the location of python binary is not valid. you can always write:

#!/usr/bin/env python

to have local environment to decide for you.

Upvotes: 1

user513418
user513418

Reputation:

If you want to run your script with shebang, you'll have to make sure that the user running the script has execution rights on the script in question (chmod u+x script.py). That's not the case if you call python with the script as an argument.

Another issue with the shebang solution is that you're forcing the location of the python executable. If I try to run your script and my version of python is in /usr/bin/python instead of /usr/local/bin/python, I'll have to edit your script. On other platforms, such as Windows, I'll have to edit it too.

Upvotes: 2

metatoaster
metatoaster

Reputation: 18908

If you are treating python as scripting language that one might execute from a POSIX compliant shell then it really doesn't matter.

That said, given that Python is more or less becoming standardized with its package management and installation, I would argue none of the above. What should be done is that all scripts be turned into callables (either functions or callable class instances) that live within modules (preferably defined a namespace package). That callable is the entry point to your program/script. Then a setuptools compliant setup.py should be placed in the root directory of the project for this module and define that as the entry point to your program.

An example:

In example.package/example/package/script.py:

def main():
    print('hello world')

The setup.py:

from setuptools import setup, find_packages

setup(
    name='example.package',
    description='An example package with an entry point',
    ...
    entry_points="""
    # -*- Entry points: -*-
    [console_scripts]
    script = example.package.script:main
    """,
)

Note that the script is defined to be example.package.script:main, where the element before the : corresponds to the package and the second element is the callable. In this case, it corresponds to the main function in example/package/script.py (standard python module layout).

Installing the package (preferably in a virtualenv) will generate the correct "executable" that will be immediately accessible from the shell, across all operating systems (i.e. on Windows, a script.exe will be created that basically will start Python with the script).

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798814

With the shebang the script must be located on a volume that permits execution, must be allowed to execute by any security modules, and the interpreter specified in the shebang must be present.

Invoking the interpreter directly only requires the script to be readable.

Upvotes: 1

Related Questions