300-435 · Question #67
What is the best way to specify the location of Python within a script?
The correct answer is B. #!/usr/bin/env python. The best practice for specifying the Python interpreter in a script is to use #!/usr/bin/env python, which ensures portability by resolving the interpreter through the system's PATH environment variable.
Question
Options
- A#!/usr/bin/env bash
- B#!/usr/bin/env python
- C#!/usr/loca l/bin/python
- D#!/usr/bin/python
- E#!/scriptname
How the community answered
(40 responses)- A3% (1)
- B90% (36)
- C5% (2)
- D3% (1)
Why each option
The best practice for specifying the Python interpreter in a script is to use `#!/usr/bin/env python`, which ensures portability by resolving the interpreter through the system's PATH environment variable.
`#!/usr/bin/env bash` specifies the Bash interpreter, not Python.
The shebang `#!/usr/bin/env python` is the most portable and recommended way to specify the Python interpreter. It instructs the operating system to search the user's PATH for the `python` executable, allowing the script to execute with the correct Python version regardless of its specific installation location or virtual environment context.
`#!/usr/local/bin/python` is a hardcoded path that may not be universally correct or suitable for virtual environments.
`#!/usr/bin/python` is a hardcoded path that may not be universally correct or suitable for virtual environments.
`#!/scriptname` is not a valid shebang line for specifying an interpreter.
Concept tested: Python script shebang portability
Source: https://docs.python.org/3/howto/unix.html
Topics
Community Discussion
No community discussion yet for this question.