Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Created November 7, 2012 15:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save LeZuse/4032238 to your computer and use it in GitHub Desktop.
Save LeZuse/4032238 to your computer and use it in GitHub Desktop.
WSGI script with virtualenv activation with Flask
import os
import sys
# Install venv by `virtualenv --distribute venv`
# Then install depedencies: `source venv/bin/active`
# `pip install -r requirements.txt`
activate_this = '/var/www/apache/csshat.com/csshat.com/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
path = os.path.join(os.path.dirname(__file__), os.pardir)
if path not in sys.path:
sys.path.append(path)
# The application object is used by any WSGI server configured to use this
# file.
# Ensure there is an app.py script in the current folder
from app import app as application
@harshal-dhumal
Copy link

harshal-dhumal commented Jan 31, 2017

This didn't work for me on python 3.4 until I added below lines.

import site
site.addsitedir('<PATH-TO-VIRTUALENV>/lib/python3.4/site-packages')

@PankajWorks
Copy link

PankajWorks commented May 31, 2017

As per the documentation for python 3 you will have to use something like

activate_this = '/var/www/apache/csshat.com/csshat.com/venv/bin/activate_this.py'
with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

@lafrech
Copy link

lafrech commented Jul 5, 2019

https://stackoverflow.com/a/24261031/4653485 presents an alternative way using runpy:

import runpy
runpy.run_path("/var/www/apache/csshat.com/csshat.com/venv/bin/activate_this.py")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment