Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am invoking cmd from Python like this:

subpocess.Popen(['coffee'], shell=True)

which I belive is translated to:

/bin/sh -c "coffee"

From docs I have read that in non-interactive mode files like /etc/profile, /etc/bash.bashrc are not read and default $PATH is used (init $PATH). Am I wright? Is there the only way to add coffee to the $PATH is to copy it to /usr/local/bin?

share|improve this question
up vote 2 down vote accepted

You can modify the PATH environment variable in your Python script before starting the shell, i.e., some variant of:

import os

os.environ['PATH'] = '/some/new/path:%s' % os.environ['PATH']
subpocess.Popen(['coffee'], shell=True)

Note that setting shell=True is very often unnecessary, and you should be aware of the implications of doing so, especially if you are calling Popen using user-supplied data. The general problem is that data containing shell metacharacters (e.g., ">", or ";", "&") and so forth can cause unexpected behavior.

UPDATE: For example, if I have a script called /tmp/foo and I want to run this using subprocess.Popen, this works just fine:

>>> import os, subprocess
>>> os.environ['PATH'] = '/tmp:%s' % os.environ['PATH']
>>> subprocess.Popen('foo', shell=False)

If I don't set PATH, then I get the expected error:

OSError: [Errno 2] No such file or directory
share|improve this answer
Bad thing with shell=False is that it simply does not work (OSError: directory of file not found). I guess it requires absolute path. – Vladimir Jun 13 '11 at 15:19
You just need to set your PATH environment correctly, as discussed in my answer. I've updated it with this exact example. – larsks Jun 13 '11 at 15:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.