title: Environment variables not setting in Python
body: <p>I'm trying to run a python script that requires certain environment variables be set. I'm currently running a .bat script that sets these environment variables and then calls the python script. </p>
  
  <p>I'd like to get rid of the .bat script and set the required environment variables within the Python script. </p>
  
  <p>This is what the .bat script looks like - </p>
  
  <pre><code>SET OSGEO4W_ROOT=C:\Program Files\QGIS 2.18
  SET QGISNAME=qgis
  SET QGIS=%OSGEO4W_ROOT%\apps\%QGISNAME%
  set QGIS_PREFIX_PATH=%QGIS%
  
  CALL "%OSGEO4W_ROOT%\bin\o4w_env.bat"
  
  set PATH=%OSGEO4W_ROOT%\bin;%QGIS%\bin;%PATH%
  SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
  set PYTHONPATH=%QGIS%\python;%PYTHONPATH%
  
  python D:\temp\fran\NEW\new.py
  </code></pre>
  
  <p>Here is how I'm trying to mimic in Python - </p>
  
  <pre><code>import os
  import subprocess
  
  OSGEO4W_ROOT = r'C:\Program Files\QGIS 2.18'
  QGISNAME = r'qgis'
  QGIS = r'{}\apps\{}'.format(OSGEO4W_ROOT,QGISNAME)
  QGIS_PREFIX_PATH = '{}'.format(QGIS)
  
  bat_command = [r'{}\bin\o4w_env.bat'.format(OSGEO4W_ROOT)]
  subprocess.check_call(bat_command)
  
  PATH = os.environ['PATH']
  PYTHONPATH = os.environ['PYTHONPATH'] 
  
  os.environ['PATH'] = r'{}\bin;{}\bin;{}'.format(OSGEO4W_ROOT,QGIS,PATH)
  os.environ ['PYTHONHOME'] = r'{}\apps\Python27'.format(OSGEO4W_ROOT)
  os.environ ['PYTHONPATH'] = r'{}\python;{}'.format(QGIS,PYTHONPATH)
  
  from qgis.core import (QgsProject, QgsComposition, QgsApplication, QgsMapLayerRegistry)
  </code></pre>
  
  <p>This is the error message I receive -</p>
  
  <pre><code>ImportError: No module named site
  </code></pre>
  
  <p>When I launch the script from the .bat file everything works. When I set environment variables in python and run I get an import error. </p>
  
  <p>Any ideas?</p>