1. Home
  2. Data Science
  3. Python
  4. How can you get an environment variable without raising…

Added 25 Nov 2026

How can you get an environment variable without raising KeyError if missing in Python?

  1. os.environ.get('NAME') Correct answer
  2. os.environ['NAME']
  3. os.getenvvar('NAME')
  4. os.env('NAME')

Correct answer

A. os.environ.get('NAME')

5 views

MCQSEXAM

Explanation

dict-style get() returns None when the variable is missing; os.getenv works too.

Reference

Asked in: Python Interview Standard Library

https://docs.python.org/3/library/index.html

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More Python MCQs

Practise all Python MCQs

Similar MCQs