title: Passing environment variables to `subprocess.call`
body: <p>I have a bash script <code>my_script.sh</code> containing a line like <code>./my_python_script.py 2&gt;&amp;1</code> and within the Python script is contained the lines</p>
  
  <pre><code>import os
  from util import do_something
  project_dir = os.environ["PROJECT_DIR"]
  do_something(spider_name="my_spider", project_dir=project_dir, data_dir="tmp/")
  </code></pre>
  
  <p>where <code>do_something</code> is something like</p>
  
  <pre><code>import subprocess
  def do_something(spider_name, project_dir, data_dir):
    subprocess.call(["scrapy", "crawl", spider_name, 
      "--set", "FEED_URI=%s%s%s" % (project_dir, data_dir, spider_name+".json"),
    cwd=project_dir+"scrapers/"+spider_name+"/")
  </code></pre>
  
  <p>I'm calling <code>my_script.sh</code> in a cron job like so:</p>
  
  <pre><code>0 13 * * * . /home/ubuntu/.profile &amp;&amp; cd $HOME/project_dir/ &amp;&amp; ./my_script.sh &gt; logs/daily_$CURR_DATE.log 2&gt;&amp;1
  </code></pre>
  
  <p>My problem is that the top level script is able to see the environment passed to it, but <code>do_something</code> is somehow not able to use it, reporting that </p>
  
  <pre><code>Traceback (most recent call last):
    File "./my_python_script.py", line 19, in &lt;module&gt;
      project_dir=project_dir, data_dir="tmp/")
    File "/home/ubuntu/project_dir/util/util.py", line 41, in 
  do_something
      cwd=project_dir+"scrapers/"+spider_name+"/")
    File "/usr/lib/python2.7/subprocess.py", line 523, in call
      return Popen(*popenargs, **kwargs).wait()
    File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
      errread, errwrite)
    File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
      raise child_exception
  OSError: [Errno 2] No such file or directory
  </code></pre>
  
  <p>The directory containing the Scrapy project does, in fact, exist, and I'm able to run <code>my_script.sh</code> manually without problem.  If I do a </p>
  
  <pre><code>print os.listdir(project_dir+"scrapers/"+spider_name+"/")
  </code></pre>
  
  <p>right before the call to <code>subprocess.call</code> I can see the complete contents of the project's directory.  </p>
  
  <p>What is going on here?  Why isn't <code>subprocess</code> able to change directories when I'm calling the script from a cronjob, but is able to when I run the script manually?  I'm really at a loss.  Thanks in advance for any insight.  </p>