What is the difference between launching. /file.py and python file.py?

When I run a Python script from the command line

./file.py

it is interpreted differently (with error with error) when I run it using:

python file.py

Why are they performed differently?

+4
source share
1 answer

On Unix-like systems:

  • ./file.pyrequires execution file.py(e.g. chmod a+x file.py).
  • ./file.pyruns the script with any interpreter specified in its shebang line; python file.pyruns it with any interpreter with the name pythonthat is highest on yours $PATH. If you have multiple versions of Python, this can make a big difference.

, python $PATH, which python, .

, shebang python, $PATH, :

#!/usr/bin/env python

Windows:

  • ./file.py script , *.py, python file.py python.exe, %PATH%. , Python, .

, Windows cmd.exe, Unix, shebangs, . Python, , *.py, PEP 397 Python, shebangs. ( launcher Python.)


:

,

, shebang . , Unix- , ./file.py , /bin/sh Unix Unix-, Python. , , , , , - import: command not found.

+9

All Articles