nerdexam
Cisco

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.

Network Automation Foundation

Question

What is the best way to specify the location of Python within a script?

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)
  • A
    3% (1)
  • B
    90% (36)
  • C
    5% (2)
  • D
    3% (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.

A#!/usr/bin/env bash

`#!/usr/bin/env bash` specifies the Bash interpreter, not Python.

B#!/usr/bin/env pythonCorrect

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.

C#!/usr/loca l/bin/python

`#!/usr/local/bin/python` is a hardcoded path that may not be universally correct or suitable for virtual environments.

D#!/usr/bin/python

`#!/usr/bin/python` is a hardcoded path that may not be universally correct or suitable for virtual environments.

E#!/scriptname

`#!/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

#Python scripting#Shebang#Script portability

Community Discussion

No community discussion yet for this question.

Full 300-435 Practice