1

Why does the following not work?

$ python -c 'import os; print os.environ['PATH']'
Traceback (most recent call last):
   File "<string>", line 1, in <module>
NameError: name 'PATH' is not defined

whereas

$ python

>>> import os
>>> print os.environ['PATH']

works?

1 Answer 1

2

Quotes. Try:

python -c 'import os; print os.environ["PATH"]'

The shell is stripping out the single quotes before the command gets to Python, as you can see with the following command:

echo 'import os; print os.environ['PATH']'

(In fact, you can see by the syntax coloring here that something weird is going to happen.)

Now, why does this happen? The single quotes around PATH end a string and start a new one. Essentially, the shell is parsing it as the following three strings (although without the line breaks):

import os; print os.environ[
PATH
]

And that's what gets passed to Python, which sees PATH as a variable name, and rightly complains that you haven't defined it.

For more information on using your shell, see man bash. ("How do I use my shell?" is not, strictly speaking, a Python question, or even a programming question.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.