Python WSGI


  1. Open Web Platform Installer and install "Python project":
  2. Follow steps in "Creating generic WSGI project":
  3. Create file with the following content:
    def application(environ, start_response):
        """WSGI Application"""
        start_response('200 OK', [('Content-type','text/html')])
        yield '<h2>WSGI environment variables</h2>\n<pre><code>'
        sorted_envs = environ.keys()[:]
        for k in sorted_envs:
            yield '{0:<24}: {1}\n'.format(k, environ[k])
        yield '</code></pre>'
    Output of sample WSGI application:


To deploy Python Tornado application you will need to install Python Hosting Package on a target server. Then just copy IIS web site from one machine to another.

web.config example

<?xml version="1.0" encoding="UTF-8"?>
   <application name="python.project" >
     <add name="VIRTUAL_ENV" value="%APPL_PHYSICAL_PATH%\venv" />
     <add name="PATH" value="%APPL_PHYSICAL_PATH%\venv\Scripts;%PATH%" />
     <add name="PYTHONPATH" value="%APPL_PHYSICAL_PATH%\venv\lib\site-packages;%APPL_PHYSICAL_PATH%\venv\lib;%APPL_PHYSICAL_PATH%;%APPL_PHYSICAL_PATH%\project" />
     <!-- generic wsgi app -->
     <add name="WSGI_APP" value="wsgi.application" />
     <!-- web console -->
     <!-- security rules for console are placed in /console/web.config -->
     <add name="CONSOLE_URL" value="console" />
     <!-- application deployment -->
     <add name="DEPLOY_FILE" value="" />
     <!-- <add name="DEPLOY_LOG" value="deploy.log" /> -->
     <!-- logging -->
     <add name="ERROR_LOG_DIR"  value="log" />
     <!-- reload on files changes -->
     <!-- <add name="WATCH_FILE_CHANGES_MASK" value="*.py" /> -->
   <!-- Django or WSGI application over fastcgi -->
   <add name="python.project#x86" scriptProcessor="python.2.7.wsgi" path="*" verb="*" modules="HeliconZoo_x86" preCondition="bitness32" resourceType="Unspecified" requireAccess="Script" />
   <add name="python.project#x64" scriptProcessor="python.2.7.wsgi" path="*" verb="*" modules="HeliconZoo_x64" preCondition="bitness64" resourceType="Unspecified" requireAccess="Script" />

Environment variables

  • PYTHONPATH — path to Python modules. In the example it points to application root, the python_modules\Lib\site-packages subfolder and also includes value defined by user.
  • WSGI_APP — full path to the application’s entry point function.
  • WATCH_FILE_CHANGES_MASK — optional files mask to watch for changes. Worker is restarted every time a file of specified type changes. In the example all .py files are watched.

web.config to disable Python in directory with static files

<?xml version="1.0" encoding="UTF-8"?>
      This removes Helicon Zoo handler and makes IIS processing static files.
      <remove name="python.project#x64" />
      <remove name="python.project#x86" />
</configuration> file example

Deploy file is used to execute commands, such as components installations or database migrations, on a server when the application initially starts or updated. The following code is only an example. We recommend you to use file that comes with the Python project template.

import sys
import os
import os.path

VIRTUALENV_EXE = os.path.join(os.path.dirname(sys.executable), 'scripts\\virtualenv.exe')
PROJECT_DIR = os.path.dirname(__file__)

def run(command, exit_on_error=True):
    print('\nRunning command: '+command)
    status = os.system(command)
    if status != 0:

def main():

    # update APPDATA env for pip
    #os.environ['APPDATA'] = os.path.join(PROJECT_DIR, PYTHON_MODULES_DIR)

    # create virtual environment
    if not os.path.exists(VIRTUALENV_NAME):

    # install requirements to local folder
    run('pip install --requirement=requirements.txt')

    # that's all
    print "Bye!"

if __name__=='__main__':