Bird
0
0

You want to check if NumPy is installed and import it safely without crashing your program. Which code snippet achieves this?

hard📝 Application Q8 of 15
NumPy - Fundamentals
You want to check if NumPy is installed and import it safely without crashing your program. Which code snippet achieves this?
Aimport numpy print('NumPy imported')
Bimport numpy as np if np is None: print('NumPy missing')
Cif 'numpy' in sys.modules: import numpy as np
Dtry: import numpy as np except ModuleNotFoundError: print('NumPy not installed')
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe import

    Using try-except catches errors if NumPy is missing.
  2. Step 2: Analyze options

    try: import numpy as np except ModuleNotFoundError: print('NumPy not installed') uses try-except for ModuleNotFoundError, which is correct.
  3. Final Answer:

    try-except block to import NumPy safely -> Option D
  4. Quick Check:

    Use try-except to handle missing packages [OK]
Quick Trick: Use try-except to import packages safely [OK]
Common Mistakes:
  • Checking np is None (never None)
  • Assuming sys.modules check imports
  • Ignoring import errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes